Question

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).

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top