Search code examples
androidandroid-fragmentsfragmentmanagerfragment-backstack

Fragment is not visible after popbackstack


I add fragment (home fragment) in Activity.onCreate(), without adding it to backstack, I do it using FragmentTransaction.replace(). After that i add every next fragment using FragmentTransaction.replace() and adding it to backstack.

I have a functionality that clears the backstack, leaving home fragment visible, to do it I use FragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);.

If i add only 1 fragment to backstack and then clear the backstack it works fine, but if I add more than one then after clearing the backstack the screen is blank, though the home fragment receives onStart() and onResume() and think's it's visible, it doesnt receive onPause(), onStop() until I add another fragment again or leave the app. Also if I popBackstack() one by one (on user action) it works fine, but if I popBackstack() in loop (popping all fragments at once) it doesnt work.

Here's the code:

public void changeFragment(BaseFragment fragment, boolean addToBackStack, boolean preventDuplicate) {
    Fragment topFragment = getSupportFragmentManager().findFragmentById(fragmentContainer.getId());
    if (preventDuplicate && topFragment != null && fragment.getClass().equals(topFragment.getClass())) {
        //Prevent adding same fragment
        return;
    }

    FragmentTransaction transaction =
            fragmentManager
                    .beginTransaction()
                    .replace(fragmentContainer.getId(), fragment);

    if (addToBackStack) {
        transaction.addToBackStack(null);
    }

    transaction.commit();
}

public void goToHome() {
    fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}

Solution

  • Turns out new support library version is messing things up. After changing

    compile 'com.android.support:appcompat-v7:25.1.1'
    

    back to

    compile 'com.android.support:appcompat-v7:25.0.1'
    

    everything works fine