2

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.

  • A <- C : C go back to the root fragment A
  • A <- B : B go back to the root 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!

Imene Noomene
  • 3,035
  • 5
  • 18
  • 34
  • share your xml file. – Hemant Parmar Nov 10 '17 at 09:09
  • In the xml file , I just add a simple textview in fragment and a frameLayout in the activity. Anyway , this is the link of the example: https://github.com/imen-nmn/BackStackExample – Imene Noomene Nov 10 '17 at 09:11
  • i check your code, i have missed to set background color in your fragment xml file.that's why its overlapping. – Hemant Parmar Nov 10 '17 at 09:16
  • set the background color to parent view of your fragment A, B ,C, you will never face this problem again – yashkal Nov 10 '17 at 09:17
  • Adding color will not remove fragments from backstack. You need to call add (instead of replace ) for first fragment and then use replace for all other fragments. OnBack press use code getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); – Radheshyam Singh Nov 10 '17 at 09:20
  • @RadheshyamSingh Unfortunally , this does not work . Also, also I want to found a solution without modifying the default behavior of onBackPressed() Because I could add another fragments in the scenario – Imene Noomene Nov 10 '17 at 09:50
  • You have to override backpressed unfortunately – Sharath kumar Nov 10 '17 at 09:56

2 Answers2

1

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();
    }
}
Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
  • It is right that when I go back from C , I redirect me to A . But when I go back from B , It stucks in B. Also, I'm searching an explanation, why C is not cleared from backstack in my example ? – Imene Noomene Nov 10 '17 at 10:53
  • sorry that was a slight mistake..see the updated code – Sharath kumar Nov 10 '17 at 10:56
  • 1
    You are not adding C to backstack..Hence it wont be removed onBackpress.The fragment that are added to backstack are only the ones that will be cleared when the back is pressed – Sharath kumar Nov 10 '17 at 10:57
  • Go through the difference between add,replace and addstacktobackstack.Then you will clearly understand it.Hope my answer helps you:) – Sharath kumar Nov 10 '17 at 10:59
  • Thanks for the answer . It inspired me for a general solution that I will post it . – Imene Noomene Nov 10 '17 at 11:02
  • 1
    Your welcome.You can override backpress but should not mess with the android system backpress method thatz it.If you are adding more fragment you can check and remove fragment in backpress. – Sharath kumar Nov 10 '17 at 11:04
0

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);
        }

    }
Imene Noomene
  • 3,035
  • 5
  • 18
  • 34