简体   繁体   中英

Cancel a CountDownTimer in another activity

I have a CountDownTimer in one activity that I need to cancel in a different activity. The problem I'm having is that the CountDownTimer is not getting cancelled even when I make a call to the endTestTimer() method in another activity. Here is the code for the CountDownTimer:


private long testMillisLeft, questionMillisLeft;
private static CountDownTimer testTimer;

private void startTestTimer()
    {
        long startTime = DataHolder.getTestTime()*1000; //getTestTime() is the seconds
        testTimer = new CountDownTimer(startTime, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                testMillisLeft = millisUntilFinished;
                updateTestTimeLeft();
            }

            @Override
            public void onFinish() {
                Intent intent = new Intent(MainActivity4.this, MainActivity5.class);
                startActivity(intent);
            }
        }.start();

    }

    public static void endTestTimer()
    {
        if(testTimer != null)
        {
            testTimer.cancel();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

        testTimeLeft = (TextView) findViewById(R.id.testTimerTextView);
        mathProblem = (TextView) findViewById(R.id.mathProblemTextView);

        testTimer = null;
        startTestTimer();

    }

And here is how I am trying to cancel the same timer in another activity:


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MainActivity4.endTestTimer();
    }

A comment on this related post says that the static CountDownTimer creates memory leak. If that's the case, how can I call the endTestTimer() method in another activity and have it cancel the timer? Alternatively, how can I access the testTimer CountDownTimer directly and call.cancel() on it in another activity if it can't be static?

Looking into Android lifecycles led me to this post and this post , both of which I found very helpful. To my understanding, when the user hits the back button, the onDestroy() method is called for the currently-displayed activity, and then the onCreate() method is called for the activity to be displayed. So, to cancel the timers on MainActivity4 (the current activity in my case), I added this code to its class file:

@Override
    protected void onDestroy() {
        testTimer.cancel();
        questionTimer.cancel();
        super.onDestroy();
    }

Now, when the user backs out of MainActivity4 (regardless of what other activity that takes them to) and onDestroy() is called automatically, both timers are cancelled and then MainActivity4 is destroyed. This seems to work fine for me, but I'm not sure if there are any downsides to doing it this way, so please let me know if there are.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM