First, my app has structure like this:
SpashActivity -> MainActivity -> switching between many fragments
My app use SlideMenu to switch
Possible problem with reading fragment stat between detach and attach.
Try to use something like this:
FragmentTransaction ft = fragmentManager.beginTransaction();
//ft.detach(fragment).attach(fragment).commitAllowingStateLoss(); // crashing
ft.replace(getView().getId(), fragment).commitAllowingStateLoss(); // not crashing
Try to change
@Override
public void onBackPressed() {
if (0 == getSupportFragmentManager().getBackStackEntryCount()) {
to
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() < 1 ) {
I hope this works
The state of your fragments is automatically saved and restored, there is no need to do anything in your onSaveInstanceState
method. Don't hold any references to your Fragments in your Activity (in your case currentFragment
, mContent
), if you need a certain Fragment, get it from FragmentManager
by e.g. findFragmentByTag
.