3

I have four main fragments and one minor fragment in my app. Let's say Fragment A, B, C, D. There's a ListView at Fragment A. If user click list item, another Fragment (sub A) will be shown up. So.., if user click A -> B -> C -> D. The backstack will be D -> C-> B-> A. This is simple and easy. Here is my problem.., when user clicked a list item at Fragment A, The transition will be A -> sub A -> B -> C -> D. But I don't want sub A on backstack. My desired result is D -> C -> B -> A. I'm currently trying to skip the sub A but still couldn't find a suitable answer.

Zin Win Htet
  • 2,448
  • 4
  • 32
  • 54
  • Possible duplicate of [Clear back stack using fragments](http://stackoverflow.com/questions/6186433/clear-back-stack-using-fragments) – Victor Viola Feb 19 '16 at 11:44
  • Possible duplicate of [Removing a Fragment from the back stack](http://stackoverflow.com/questions/9033019/removing-a-fragment-from-the-back-stack) – Robin Vinzenz Feb 19 '16 at 11:44

3 Answers3

2

Use this by overriding or any specific back button Click

FragmentManager fm = getActivity().getSupportFragmentManager();
fm.popBackStack();

Override back button like this

// 2.0 and above
@Override
public void onBackPressed() {
    moveTaskToBack(true);
}

// Before 2.0
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
Nuke
  • 406
  • 3
  • 13
1
  1. Create a boolean flag in your hosting activity to track whether Fragment sub A should be skipped:

private boolean skipFragment = false;

  1. When navigating from Fragment sub A to any other fragment, set the skipFragment flag to true:

skipFragment = true;

  1. In the hosting activity, override the onBackPressed() method to handle the back button press:

     @Override
     public void onBackPressed() {
     FragmentManager fragmentManager = getSupportFragmentManager();
    
     // Check if Fragment should be skipped
     if (skipFragment) {
         // Clear the back stack up to Fragment A
         fragmentManager.popBackStack("FragmentATag", FragmentManager.POP_BACK_STACK_INCLUSIVE);
         skipFragment = false; // Reset the flag
         return;
     }
    
     super.onBackPressed();
    }
    

Make sure you replace "FragmentATag" with the actual tag or name used for Fragment A when it was added to the back stack.

Fragment selectedFragment = new FragmentA();
    MainActivity.skipFragment = true;
    getFragmentManager().beginTransaction()
            .replace(R.id.fragment_container, selectedFragment)
            .addToBackStack("FragmentATag")
            .commit();
Bianca Balan
  • 321
  • 5
  • 14
0

I think below code is working for you.

FragmentManager fManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fManager.beginTransaction();
trans.remove(myFrag);//fragment you want to remove 
trans.commit();
fManager.popBackStack();

Suggestion

You should use Navigation component. It's very easy and more readable. If you use Navigation Component you can easily handle this type of case, while also helping you visualize your app’s navigation flow.

Try Navigation Component

Atik Faysal
  • 158
  • 1
  • 13