-
Bundle savedInstanceState is a reference to the Bundle object that is passed to the
onCreate()in an Activity. Activities have capability to restore themselves to a previous state using the stored in the Bundle. -
First time the
bundleobject is alwaysnull. If some data is stored in the bundle object during the Activity lifecycle and Activity is recreated due to rotation then thebundleobject will benon-null.During the Activity Lifecycle save the data in
onSaveInstanceState()int scrolledPosition = 0; @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("scrolled_position",scrolledPosition); }After the orientation when the Activity is recreated and
onCreate()is calledint scrolledPosition = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(savedInstanceState == null){ scrolledPosition = 0; }else{ scrolledPosition = savedInstanceState.getInt("scrolled_position",0); } }There is an alternative way of restoring state ,
onRestoreInstanceState()which system calls afteronStart(). The system callsonRestoreInstanceState()only if there is a state to restore. So there is no need to check ifbundleobject isnull.int scrolledPosition = 0; public void onRestoreInstanceState(Bundle savedInstanceState) { // Always call the superclass so it can restore the view hierarchy super.onRestoreInstanceState(savedInstanceState); // Restore state members from saved instance scrolledPosition = savedInstanceState.getInt("scrolled_position",0); }Note: Always call the superclass implementation of
onRestoreInstanceState()so the default implementation can restore the state of the view hierarchy.What kind of data should be stored in
bundleobject-
User selections
-
Scroll positions
-
User submitted data
What kind of data shouldn’t be stored in
bundleobject-
Bitmap
-
File
-
Big Model classes(POJO)
Reference Link:
https://developer.android.com/guide/components/activities/activity-lifecycle#restore-activity-ui-state-using-saved-instance-state
-
Share this