Confusing Android Activity lifecycle bahavior
I did a simple program by overriding each of the lifecycle methods and everything works as I'd expect, except for onRestoreI开发者_如何学JAVAnstanceState(Bundle savedInstanceState)
.
I overrode the method as
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Toast.makeText(getBaseContext(), "onRestoreInstanceState - Activity1 ", Toast.LENGTH_SHORT).show();
}
But it never gets called.
I overrode onSaveInstanceState(Bundle outState)
too and I can see the system calling it, but never the onRestoreInstanceState(Bundle savedInstanceState)
. When I press back button or so, I can see that the system calls onRestore()
, OnStart()
and onResume()
, and the UI (just two buttons) are displayed correctly.
How is it possible that the UI is restored without calling onRestoreInstanceState(Bundle savedInstanceState)
? I only do setContentView(R.layout.main)
is onCreate(savedInstanceState)
. So how does it restore the UI without calling either onCreate()
or onRestoreInstanceState()
?
Would really appreciate somebody shedding some light on this.
Thank you.
The documentation here is a bit confusing, but I think this method is only intended as a kind of stand-in for onCreate's handling of stored state, occurring later in the cycle. It's only called if the activity is destroyed and re-created with state; this would most commonly happen during an orientation change. It's not intended to handle state during ordinary transitions between activities.
See also onSaveInstanceState () and onRestoreInstanceState ()
精彩评论