I'm switching between fragments by hiding the last fragment and adding a new one (See code below) - adding it to the back-stack as well. This way, users can quickly switch between the fragments without reloading the fragment data.
This works well until the app is killed (Scenario: users uses several other apps and my app is getting persisted and killed).
When a user opens the app, it is being restored and all the fragments are shown - overlapping one another.
Question: How can the restored fragments be restored with their hidden state? Perhaps I'm missing some flag? somewhere? Perhaps there is a better solution for fast switching between fragments (without reloading the data)?
Sample code of adding fragments - invoked several times with different fragments upon clicking somewhere:
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.hide(lastFragment);
fragmentTransaction.add(newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
lastFragment = newFragment;
I met the same problem,I think this is Android framework bug.Here is the issue.
However my way will work for you, we should override the
onSaveInstanceState(Bundle outState)
method, save our custom data tooutState
, but never to callsuper.onSaveInstanceState(outState);
.