Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why onRestoreInstanceState() never gets called

Tags:

android

I am trying to save data in my activity and than restore it. I save data in on onSaveInstanceState() and then I try to restore the data in onRestoreInstanceState().

I setup breakpoint, the method onSaveInstanceState() get called. But onRestoreInstanceState() or onCreate() never did.

Here is my steps:

  1. start my Activity.
  2. press 'Home' button on the phone. onSaveInstanceState() get called.
  3. Click the icon in the launcher and launch my Activity again.

At this time, only onRestart() get called. But not onRestoreInstanceState() or onCreate().

Does anyone know why?

like image 828
michael Avatar asked Apr 07 '11 00:04

michael


People also ask

When in the activity lifecycle is onRestoreInstanceState () called?

This method is called after #onStart when the activity is being re-initialized from a previously saved state, given here in <var>savedInstanceState</var>. This is the same as #onRestoreInstanceState(Bundle) but is called for activities created with the attribute android. R.

When onSaveInstance() is called?

Note that onSaveInstanceState() is called when your activity goes into the background and NOT when the app process is about to be killed.

When on save instance state is called android?

The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.


2 Answers

Well, if onRestart() is called, the value of the instance variables would be maintained by the application stack itself and thus you do not need to restore them.

onCreate() method is only called when your Activity's onStop() is called and the process is killed.

Please refer the Activity life cycle Android Activity Life Cycle for a clear understanding.

You may want to check whether the onStop() method is called and if your process is killed. I do no think that your process gets killed by the scenario which you have described.

the onRestoreInstanceState() method is very tricky. I do not know when exactly it is called but I saw it was called once while changing from Potrait to Landscape.

like image 57
chaitanya Avatar answered Sep 24 '22 17:09

chaitanya


From doc:

The system calls onRestoreInstanceState() only if there is a saved state to restore.

like image 32
Goran Horia Mihail Avatar answered Sep 20 '22 17:09

Goran Horia Mihail