Imene Noomene
Imene Noomene

Reputation: 3053

Fragment does not pop out from backstack?

I have three fragment A, B and C

enter image description here This is the path : A -> B -> C : A go to B and B go to C

When I go back from the fragment C , I want to go to the fragment A.

But My problem is when I pressed back in the fragment C , I get this behviour : It seems that the fragment C is not cleared from the backstack : enter image description here

As for my code , this is the method of the replaceFragment :

public void replaceFragment(Fragment fragment, boolean withBackStack) {
    android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    String fragmentTag = fragment.getClass().getName();
    fragmentTransaction.replace(R.id.frame, fragment, fragmentTag);
    if (withBackStack)
        fragmentTransaction.addToBackStack(fragmentTag);
    try {
        fragmentTransaction.commitAllowingStateLoss();
    } catch (IllegalStateException ex) {
        ex.printStackTrace();
    }
}

First I called replaceFragment(new AFragment(), false); in the MainActivity , Then in the Fragment A when I clicked in the button, I called mListener.replaceFragment(new BFragment(), true); Finally , in the Fragment B when I clicked the button , I called mListener.replaceFragment(new CFragment(), false);

Does anyone have an explanation for this behaviour ? The last fragment C shouldn't be cleared when I click backpressed ?

Here , you will find the whole example. Thanks in advance!

Upvotes: 2

Views: 3076

Answers (2)

Imene Noomene
Imene Noomene

Reputation: 3053

Based on the answer of @Anonymous, I found a general solution for this problem :

  @Override
    public void onBackPressed() {
        Fragment currentFragment= getSupportFragmentManager().findFragmentById(R.id.frame);
        super.onBackPressed();

        if(currentFragment!=null && currentFragment.isVisible()) {
            getSupportFragmentManager().beginTransaction().remove(currentFragment).commit();
            getSupportFragmentManager().popBackStack(currentFragment.getTag(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }

    }

Upvotes: 0

Sharath kumar
Sharath kumar

Reputation: 4132

Since you are not adding fragC to the backstack when backpressed wont pop the stored transaction it is not removed.instead here you have to remove the fragment by overriding backpress.Overiride backpress and check for the fragC and remove it and then call the pospstack to pop back the stored transaction.

Also store the instance of fragment as global to check if the fragment if fragC.

private Fragment mFragment;

Inside you method store the instance

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(mFragment.getClass().getName());
        if (fragment != null && fragment.getClass().getName().equalsIgnoreCase(CFragment.class.getName())) {
            getSupportFragmentManager().beginTransaction().remove(mFragment).commit();
        }
        getSupportFragmentManager().popBackStackImmediate();
    } else {
        super.onBackPressed();
    }
}

Upvotes: 1

Related Questions