0

I am trying to implement a view with a text view counting down a number every 600 miliseconds and dynamically displaying the number in text view.

The problem: the activity gets called as expected, but during countdown the activity gets created again and the countdown begins from the start. The OnCreate() method in my activity gets called twice although it should be called once. I know it could be triggered by a change in screen orientation and other configuration changes, but it doesn't seem to happen here.

OnCreate() in my activity:

protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.SqueezeLayout);
        countdownTextView = (TextView)FindViewById(Resource.Id.countdown_text_view);
        Timer myTimer = new Timer(18000, 600, this);
        myTimer.Start();
    }

My implementation of CountDownTimer:

class Timer : CountDownTimer
    {
        public int count = 30;
        SqueezeActivity squeezeActivity;

        public Timer(long totaltime, long interval, SqueezeActivity activity)
                                 : base(totaltime, interval)
        {
            squeezeActivity = activity;
        }

        public override void OnTick(long millisUntilFinished)
        {
            count--;
            String countStr = count.ToString();
            squeezeActivity.countdownTextView.Text = countStr;
        }

        public override void OnFinish()
        {
            squeezeActivity.StartActivity(typeof(AnotherActivity));
        }

    }

I am a beginner in Xamarin development so please be forgiving :) Thanks.

user1743439
  • 43
  • 1
  • 7
  • Take a look to this, I think that your problem may be related with it: http://stackoverflow.com/questions/3588682/is-it-normal-for-the-activity-oncreate-method-to-be-called-multiple-times – Nestoraj Aug 23 '16 at 07:25
  • I wonder how you were able to build this code & run it since you haven't implemented OnFinish() method of CountDownTimer. OnTick() & OnFinish() are required to be implemented when overriding CountDownTimer class. – Sujay Aug 23 '16 at 07:35
  • @Nestoraj, I have read that post. They suggest delaying the thread on which timer is running, but I can't delay because I'm implementing a kind of metronome, it has to be precise. I just tried starting my timer in a separate thread with the following code: `System.Threading.Thread thread = new System.Threading.Thread(new ThreadStart(runner.StartTimer));` and `thread.Start();` but the results are the same. After a bit of debugging I discovered that the activity gets recreated after starting the Timer. I have not found anything helpful in Android documentation. I will keep looking. – user1743439 Aug 23 '16 at 09:10

1 Answers1

1

As it turned out, the method called twice was caused by a simple mistake in code logic - I was calling StartActivity twice. So it had nothing to do with the countdown timer. Thanks for the answers. :)

user1743439
  • 43
  • 1
  • 7