You should search online for onSaveInstanceState
, it's a callback which is defined inside Activity
class.
It works in the same way when you need to pass data between activites
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
}
Inside outState
you will use normal Bundle methods to pass Strings, Integers and Parcelable values (putParcelableArrayList/putParcelable/putParcelableArray).
When the screen rotate or something happens (activity got destroyed etc.), this method could be called and when the activity gets recreated the bundle you used here will be passed to onCreate
inside the savedInstanceState
argument (the only argument of onCreate
, Bundle).
Here inside onCreate
you check if (savedInstanceState != null)
to be sure you have a saved state, if it's true you use savedInstanceState.getParcelableArrayList
(or something else, depends on what you want to get) to read back the list.
if (savedInstanceState != null)
{
ArrayList<Parcelable> parcelableArrayList = savedInstanceState.getParcelableArrayList("key");
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putParcelableArray(myParcelableList);
}