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:
Activity
.onSaveInstanceState()
get called.Activity
again. At this time, only onRestart()
get called. But not onRestoreInstanceState()
or onCreate()
.
Does anyone know why?
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.
Note that onSaveInstanceState() is called when your activity goes into the background and NOT when the app process is about to be killed.
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.
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.
From doc:
The system calls onRestoreInstanceState() only if there is a saved state to restore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With