KousiK
KousiK

Reputation: 825

Android Activity restoring state automatically

In my app I haven't overridden any state related methods like onPause(), onStop() but still my activity is restoring its previous state automatically in some device but in others device it's not restoring.

Why is this happening?

Here is the code:

 new CountDownTimer(30000, 1000) {

 public void onTick(long millisUntilFinished) {
      mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     mTextField.setText("done!");
 }
}.start();

Upvotes: 2

Views: 85

Answers (2)

Veaceslav Gaidarji
Veaceslav Gaidarji

Reputation: 4301

If you press Home button and then open your application, your application will be restored and timer will work from related place (when you press Home button - application works in background). But if you press Back button (application is finished) and then open application - it will be recreated/restarted and timer will start from beginning.

Upvotes: 0

Szymon
Szymon

Reputation: 43023

This is all described in this part of Android developer guide.

By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText object). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you.

So it means that the state of the views is saved automatically. It doesn't happen all the time though, for example:

When your activity is destroyed because the user presses Back or the activity finishes itself, the system's concept of that Activity instance is gone forever because the behavior indicates the activity is no longer needed.

Upvotes: 1

Related Questions