State changes while onRestoreInstanceState() has been override
As far as I understand onSaveInstanceState()
and onRestoreInstanceState()
method are intended to store dynamic data before Activity's state is about to go onPause()
. I made a simple Activity
with a button which changes its text after clicking on it. I did override both these methods with nothing in their body:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
I'm hidding and restoring the activity by means of moving to home screen and back to app. And when activity is rest开发者_开发问答ored the button's text isn't default, as I expected, but it is as it was before passing the activity to onPause()
. How does it work if my onRestoreInstanceState()
method is empty.
What is more odd - I tried to debug to see when these two methods are being invoked and noticed the onRestoreInstanceState()
wasn't even called.
intended to store dynamic data before Activity's state is about to go onPause().
Not quite. As the documentation for onSaveInstanceState()
states:
"If called, this method will occur before onStop(). There are no guarantees about whether it will occur before or after onPause()."
I'm hidding and restoring the activity by means of moving to home screen and back to app.
That may or may not trigger onSaveInstanceState()
. It is unlikely to trigger onRestoreInstanceState()
. A better way to test these methods is to change screen orientation.
And when activity is restored the button's text isn't default, as I expected, but it is as it was before passing the activity to onPause().
Users cannot type into buttons, so the default implementation of onSaveInstanceState()
ignores them.
精彩评论