Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onPause/onResume activity issues

Tags:

android

timer

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:

@Override
public void onPause()
{
    if(this._timer_time_remaining > 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 > 0) {
        start_timer(this._timer_time_remaining);
    }
    super.onResume();
}

The start_timer() method creates a CountDownTimer which updates the textview in the onTick method and updates the this._timer_time_remaining int variable.

CountDownTimer and _timer_time_remaining are both declared at the class level like this:

private CountDownTimer timer;
private int _timer_time_remaining;

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.

Thanks in advance!

Note: Edited to clean up the code copying

like image 886
Blather Avatar asked Mar 14 '10 04:03

Blather


People also ask

What is the difference between onPause and onResume?

onPause() gets called when your activity is visible but another activity has the focus. onResume() is called immediately before your activity is about to start interacting with the user. If you need your app to react in some way when your activity is paused, you need to implement these methods.

What happens in onResume?

onResume() is called whenever you navigate back to the activity from a call or something else. You can override the onResume method similarly as onCreate() and perform the task. This may help you understand the lifecycle of and Android app more.

What is the difference between onPause and onStop?

onPause()- Screen is partially covered by other new activity. The Activity is not moved to Back Stack. onPause() + onStop()- Screen is fully covered by other new activity. The Activity is moved to Back Stack.

Is onPause always called before onStop?

onPause() is always called. This is guaranteed. If you need to save any state in your activity you need to save it in onPause() . onStop() may be called after onPause() , or it may not.


2 Answers

If you take a look at the diagram for Activity lifecycle you'll realize that there are no guarantees about you Activity after onPause is called. Android could kill you Activity without even calling onDestroy. You have to save your state using onSaveInstanceState and restore it using onRestoreInstanceState.

This might be useful.

EDIT:

Lifecycle covers different scenarios. If you have only one Activity pressing Back is equivalent to exiting your application. If you have Activity A which starts activity B then onSaveInstanceState is called for Activity A. When you press back in Activity B it just exits and onRestoreInstanceState is called for Activity A. You are trying to save data between two distinct runs of your application and you need to use some kind of persistent storage for this.

like image 144
Nikola Smiljanić Avatar answered Sep 28 '22 11:09

Nikola Smiljanić


This is how I would save the value: (Four lines of code)

SharedPreferences example = getSharedPreferences("example", MODE_PRIVATE);
SharedPreferences.Editor edit = example.edit();
edit.putInt("time_remaining", (Integer)this._timer_time_remaining);
edit.commit();

On resume, do this: (Two lines of code)

SharedPreferences example = getSharedPreferences("example", MODE_PRIVATE);
int time_remaining = example.getInt("time_remaining", 0);

I adapted this from some of my working code, but did not actually test it in this exact format. Search documentation for sharedPreferences to learn how it works.

like image 45
user425923 Avatar answered Sep 28 '22 11:09

user425923