0

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?

noG23
  • 93
  • 2
  • 7
  • Is "endTestTimer()" executed somehow/sometime? (it's not the best way to share "common things" across Activities, however it should work fine) – emandt Jan 20 '22 at 14:44
  • @emandt endTestTimer() is called to in the onCreate() method of every other activity in the app by doing MainActivity4.endTestTimer(). Still, though, the timer counts down in the background. I know testTimer's value is changed, because when I remove the if statement in endTestTimer(), the app immediately crashes since testTimer is initially null. – noG23 Jan 20 '22 at 14:52
  • 1
    You need running in Debug Mode and place a breakpoint inside "endTestTimer()" to check WHEN it is called....then continue row-by-row (following the flow using F8 key) and check where you go when you press the last F8 going outside the end of that method. If you stay in MainActivity4 then it means the Timeout is too low that expires before reaching MainActivity5's "onCreated()". – emandt Jan 20 '22 at 15:02
  • @emandt Sorry, I'm new to Android Studio. How do I enter debug mode and place breakpoints in my code? Do you have an article I could read or a video I can watch? – noG23 Jan 20 '22 at 15:19
  • There is a world behind debugging, so it's impossible to explain here how to debug. Do some search on internet and maybe you can start from here: https://developer.android.com/studio/debug. Debugging in this way let you complete control to see "how is the flow of the code and which method is called before/after one other". – emandt Jan 20 '22 at 15:51
  • @emandt Thanks for the suggestion, hopefully it helps. – noG23 Jan 20 '22 at 15:57

1 Answers1

0

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.

noG23
  • 93
  • 2
  • 7