We know about the common life cycle methods of Activity but what happens when user rotates screen :
Basically whenever user rotates the screen android save the states in onSaveInstanceState() and recreate the activity by getting all view state from onCreate() bundle or from the onRestoreInstanceState() method.
onSaveInstanceState() method runs after onResume() and before onStop() but it doesn't guarantee to run before or after onPause.
onRestoreInstanceState() method runs after onStart() method of lifecycle.
Example :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mScore = savedInstanceState.getInt(SCORE);
mLevel = savedInstanceState.getInt(LEVEL);
} else {
// initialize members with default values
}}
and using onRestoreInstanceState() :
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mScore = savedInstanceState.getInt(SCORE);
mLevel = savedInstanceState.getInt(LEVEL);}
0 Comment(s)