How Can We Retain A Simple State Array Across Configuration Changes Using Headless Fragments?
I have gone through so many other similar questions but none solve my problem. One of the uses of Fragments (apparently) is to persist a state. I have a State array called arrState
Solution 1:
I finally resolved this! It is a MAJOR bug in Eclipse!! When you create a new Android Application Project in Eclipse, the wizard allows you to start with an actionbar. If you do this and also select Navigation Type "Action Bar Spinner" the wizard creates among other things the following code by default:
@OverridepublicvoidonSaveInstanceState(Bundle outState) {
// Serialize the current dropdown position.
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
.getSelectedNavigationIndex());
}
This is very kind of said wizard but there is a huge problem! There is no call to super!! The code works perfectly well in most circumstances BUT if you want to persist a fragment for your activity using SetRetainInstance(true) you need to add the "super" in as follows or it will not detect the fragment when your activity recreates.
Should be:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Serialize the current dropdown position.
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
.getSelectedNavigationIndex());
}
Hope this saves someone else the days it has cost me!
Post a Comment for "How Can We Retain A Simple State Array Across Configuration Changes Using Headless Fragments?"