0

I have three fragments (fragmentA, fragmentB, FragmentC). The code goes from fragmentA to fragmentB and then fragmentB to Fragemnt C.

When I press the back button I go from fragmentC to fragmentA.

After the back button is pressed fragmentA is displayed but you can also see fragmentC behind it.

fragmentA

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction;

ft.repalce(R.id.container, fragmentB)
  .addToBackStack("Null")  
  .commit();

fragmentB

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction;

ft.repalce(R.id.container, fragmentC)
  .commit();

fragmentC

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction;

ft.repalce(R.id.container, fragmentC)
  .commit();

When the back button is pressed I want to go from fragmentC to fragmentA and not have fragment C displayed in the background

Shawn
  • 1,222
  • 1
  • 18
  • 41
  • Possible duplicate of [How to replace fragment C with fragment A when back button is pressed?](https://stackoverflow.com/questions/13984871/how-to-replace-fragment-c-with-fragment-a-when-back-button-is-pressed) – ADM Apr 17 '19 at 04:07
  • 1
    why instantiating fragment manager, and not using FragmentManager fm = getSupportFragmentManager(); ? – Zain Apr 17 '19 at 04:09
  • Your right Essayem it should be getSupport that was a type-o, I will edit my question. – Shawn Apr 17 '19 at 04:14

3 Answers3

1

In addToBackStack use null as param , not "Null"

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction;

    ft.repalce(R.id.container, fragmentB)
      .addToBackStack(null)  
      .commit();
Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
0

you can try this when replacing fragment

Fragment frag=null;
frag=new Navigation_Help();
if(frag!=null){
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction ft=fragmentManager.beginTransaction();
        ft.replace(R.id.sacreenarea,frag);
        //for not to back previous fragment remove next nile
        ft.addToBackStack(null);
        ft.commit();
    }
Muhaiminur Rahman
  • 3,066
  • 20
  • 27
0

I tried using @ADM duplicate link, it would take me to the fragment A but if I loaded fragment B for a second time I would get double commit errors.

The solution that works for me is to restart the activity with the @Override onBackPressed(), this will reload the fragmentA by default.

Fragment B

FragmentManger fm = fragmentManager();
FragmentTransaction ft = fm.beginTransaction();
fm.replace(R.id.container, fragmentC, "fragmentC");
.commit();

fragmentC backbutton pressed

Activity

@Override
public void onBackPressed() {
    if(getSupportFragmnetManager().findFragmnetByTag("fragmentC") !=null) {
        Intent myIntent = new Intent(this, Activity.class);
        startActivity(myIntent);
        finish();
    } else {
        super.onBackPressed();
    }
}

This solution will only work if you want to go back to the first fragment loaded by the Activity, so it may not be the best solution out there but it worked for me.

Shawn
  • 1,222
  • 1
  • 18
  • 41