Note that there are some explanatory texts on larger screens.

plurals
  1. POonPause/onResume activity issues
    primarykey
    data
    text
    <p>I have a small test application I am working on which has a timer that updates a textview to countdown from 100 to 0. That works fine, but now I am trying to pause the application if the user presses the back button on the phone and then restart the timer from where they left off when they reopen the app. Here is the code I am using:</p> <pre><code>@Override public void onPause() { if(this._timer_time_remaining &gt; 0) { this.timer.cancel(); } super.onPause(); Log.v("Pausing", String.format("Pausing with %d", this._timer_time_remaining)); } @Override public void onResume() { Log.v("Resuming", String.format("Resuming with %d", this._timer_time_remaining)); if(this._timer_time_remaining &gt; 0) { start_timer(this._timer_time_remaining); } super.onResume(); } </code></pre> <p>The start_timer() method creates a CountDownTimer which updates the textview in the onTick method and updates the this._timer_time_remaining int variable.</p> <p>CountDownTimer and _timer_time_remaining are both declared at the class level like this:</p> <pre><code>private CountDownTimer timer; private int _timer_time_remaining; </code></pre> <p>From the Log.v() prints I see that the _timer_time_remaining variable has the correct number of seconds stored when onPause is called, but it is set back to 0 when onResume starts. Why does the variable get reset? I thought that the application would continue to run in the background with the same values. Am I missing something? This is all declared in a class that extends Activity.</p> <p>Thanks in advance!</p> <p>Note: Edited to clean up the code copying</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload