Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Activity Lifecycle: restoring app after kill - design problem

atm I try to get my app working around "onSaveInstanceState" and "onRestoreInstanceState", but the deeper I dig, the the more problems occur.

So, for example, I just realized, that restoring one activity via those two functions is pretty useless. Because, if I press the back button and return to the activity before, this one doesn't get its "savedInstanceState" bundle and instead gets recreated completely.

Is there a way to restore the whole application instead of just a single activity? Or is this just a weird design and I shouldn't even bother with restoring one activity?

Kind regards, jellyfish

Edit: ok, stupid me...

the bundle my main activity got was not null, but only in "onRestore...". In "onCreate" it was null indeed, but this was true all the time. (No matter if I came back from another activity after kill or not, for example)

So now I'm confused in another way: I have tested this before in another activity, and there, the savedInstanceState bundle of "onCreate" and "OnRestoreInstanceState" where the same! Is this just random or something special of the main activity? (Tried different launch modes as well, but they had no impact).

like image 543
jellyfish Avatar asked May 19 '11 08:05

jellyfish


People also ask

Is onDestroy called when app is killed?

When Android decides to kill our app, our activities will call onDestroy method. But before that, one more method will be also called and that is onSaveInstanceState(Bundle). If we check the first method, which is called when opening new activity, onCreate(Bundle), we can see the Bundle parameter.

Which life cycle methods will call when system kills the process Android?

If the activity is finishing, onDestroy() is the final lifecycle callback the activity receives. If onDestroy() is called as the result of a configuration change, the system immediately creates a new activity instance and then calls onCreate() on that new instance in the new configuration.

What is onSaveInstanceState () and onRestoreInstanceState () in activity?

The onSaveInstanceState() method allows you to add key/value pairs to the outState of the app. Then the onRestoreInstanceState() method will allow you to retrieve the value and set it back to the variable from which it was originally collected.

When Android application starts what is the first call back life cycle method to be called?

1. onCreate(): In this state, the activity is created. 2. onStart(): This callback method is called when the activity becomes visible to the user.


1 Answers

So, for example, I just realized, that restoring one activity via those two functions is pretty useless.

No, it is very useful, when used properly.

Because, if I press the back button and return to the activity before, this one doesn't get its "savedInstanceState" bundle and instead gets recreated completely.

No, it doesn't.

If it already exists and is on the back stack, it will be started (onStart()) and resumed (onResume()), but it is not created. If Android had to close the previous activity (e.g., due to a shortage of memory), the previous activity will be created (onCreate()) and will be passed a Bundle containing the data it populated in onSaveInstanceState().

The only way those statements would not be true is if you are monkeying with the BACK button processing.

Is there a way to restore the whole application instead of just a single activity?

No.

Or is this just a weird design and I shouldn't even bother with restoring one activity?

You most certainly should restore one activity.

onSaveInstanceState() is used for two scenarios:

  1. If the user changes configuration (e.g., rotates the screen), your activity will destroyed and recreated. You use onSaveInstanceState() to pass data from the old activity instance to the new one.
  2. The BACK button scenario I outlined above.

I have tested this before in another activity, and there, the savedInstanceState bundle of "onCreate" and "OnRestoreInstanceState" where the same!

Of course. They are supposed to be the same. If the activity is being created totally from scratch, onCreate() will be passed null and onRestoreInstanceState() will not be called. But if there is instance state, that state (Bundle) will be passed to both onCreate() and onRestoreInstanceState().

Is this just random or something special of the main activity?

Neither. They are supposed to be the same.

like image 168
CommonsWare Avatar answered Sep 27 '22 17:09

CommonsWare