How can I handle all states of orientation event occurs?
Something like:
- before starting (save some screen states)
- when happening (animation purposes)
- after it's happen (load the screen state)
I know that onConfigurationChanged can handle orientation changes. And I tried this:
public void onConfigurationChanged(Configuration cfg) {
saveState();
super.onConfigurationChanged(cfg);
loadState();
}
On saveState I store the lastIndex viewed on the Gallery on SharedPreferences. On loadState I get the lastIndex from the SharedPreferences and make it as the current on the Gallery.
I tried also put loadState in the onResume method but it's not called after the orientation change occurs.
You may be a little confused.
This is from the api docs:
If you've specified the parameter in your AndroidManifest, the system will notify you of orientation change by calling onConfigurationChanged(). If you did not set the flag, then Android will kill your activity start a new one with a proper orientation and then call onResume().
If you want to be notified when activity finished orientation changed and is all laid out, I would recommend overriding an onDraw() method in one of your child views. By then everything is done and you could for instance restore the state from before the orientation change.
HTH.
Use
onSaveInstanceState()
and/oronRetainNonConfigurationInstance()
and/or a fragment on which you have calledsetRetainInstance(true)
That is handled by the OS.
Use
onRestoreInstanceState()
and/orgetLastNonConfigurationInstance()
(if you went the fragment route, your data is just naturally still there)@EightEight's answer covers this nicely.