1

My app has 3 UI levels, each level has its own fragment, A -> B -> C. I wish to optionally allow my app user to navigate straight to the top level fragment i.e. from C -> A without invoking B. i.e., I still want to allow the user to go from C->B if they press the back button, but in the C fragment, I have a "Home" button, which takes them directly to A. This is the operation where I want to flush the backstack. Additionally, I want the user to be able to go from B->A using the back button, hence I'm adding A and B both to the backstack.

I have tried the options from this SO post but in each case, onCreateView gets invoked for each fragment, even if I use fm_.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

Also checked this thread, it seems to recommend using popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)

Can anyone please suggest a way to pop the entire backstack without having the onCreateView called on popped fragments?

Community
  • 1
  • 1
user90766
  • 329
  • 1
  • 4
  • 17

3 Answers3

1

Dont add backStack line for going from fragment B->C

while going from A->B use

HighlightFragment highlightFragment=new HighlightFragment(FirstReaderScreen.this);
    getSupportFragmentManager()
    .beginTransaction()
    .add(R.id.LL_Fragment, highlightFragment) // LL_Fragment is container
    .addToBackStack(null)
    .commit();

while going B->C use

  HighlightFragment highlightFragment=new HighlightFragment(FirstReaderScreen.this);
        getSupportFragmentManager()
        .beginTransaction()
        .add(R.id.LL_Fragment, highlightFragment)
        .commit();

dont add line .addtobackstack(null)

This worked for me

Manohar
  • 22,116
  • 9
  • 108
  • 144
  • I'm sorry, my post didn't completely document the workflow. I have updated it to explain the full nav. I cannot skip adding A or B from the backstack. – user90766 Dec 16 '16 at 04:20
  • refer http://stackoverflow.com/questions/7992216/android-fragment-handle-back-button-press and http://stackoverflow.com/questions/5448653/how-to-implement-onbackpressed-in-fragments , To handle back press of Fragments – Manohar Dec 16 '16 at 04:23
  • I'm already handling the back button press, as explained above, my problem is the "Go to Home" nav from C -> A, where I want to flush the backstack without invoking onCreateView on B. – user90766 Dec 16 '16 at 04:27
  • i have an idea , when ever you want to open A directly without opening B , Create a new fragment of A in C and go to fragment A and clear all back stack Fragments , Else if you want to go to B just do popupbackstack, may be it will work – Manohar Dec 16 '16 at 04:30
  • The problem is, how do I "clear all backstack fragments" ? If I try the "recommended" approaches mentioned in my original post, each of them invokes onCreateView on each fragment in the backstack. I don't want that. – user90766 Dec 16 '16 at 04:35
  • http://stackoverflow.com/questions/5802141/is-this-the-right-way-to-clean-up-fragment-back-stack-when-leaving-a-deeply-nest see this, and http://stackoverflow.com/questions/17107005/how-to-clear-fragment-backstack-in-android this Too – Manohar Dec 16 '16 at 04:41
0

i estimate you're using replace fragment. try to use begintransaction.hide / show to manage many fragment inside 1 activity if you don want to popback

//on Activity
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(containerId, new FragmentA(), fragmentA.getClass().getSimpleName());
fragmentTransaction.commit();
//inside fragment A to jump fragment without PopBackStack
getFragmentManager().beginTransaction().hide(this).commit();
getFragmentManager().beginTransaction().show(new FragmentB()).commit();
  • Sorry my post didn't fully document the nav, I've updated it. The only workaround I've found so far, is to skip A entirely from the backstack, and then add code into B & C to specifically replace B / C with A if the user clicks "Go to Home" in B/C or presses the android back button from B. But this is a hack, I would prefer a cleaner way of flushing the backstack without invoking onCreateView on B or A. – user90766 Dec 16 '16 at 04:24
0

This is working for me when needing to clear the back stack for tab switches in BottomNavigationView

public void clearBackStack() {

    int backStackEntryCount = mFragmentActivity
                                    .getSupportFragmentManager()
                                    .getBackStackEntryCount();

    for (int i = 0; i < backStackEntryCount; i++) {
        mFragmentActivity.getSupportFragmentManager().popBackStack();
    }
}
Gene Bo
  • 11,284
  • 8
  • 90
  • 137