0

I'm an android beginner and I'm struggling very hard with a problem which is that: I have an activity which manages 4 fragment. I would like that, when I add a fragment to backStack, it removes the same fragments from backStack (if there is one) in order to have only one time each fragment in backstack. If user goes from A to B to C, and goes back to B, The back stack looks like: C,A instead of C,B,A.

I've looked for a solution to my problem and I've found that a lot of people had the same propblem: Removing a Fragment from the back stack How to avoid adding same fragment to stack The most suggested solution was popBackStackImmediate(String, int) but the enormous problem is that this function removes the stacks on top of the stack that has been popped. This behaviour seems to be unchangeable. So I imagined a solution which consist in run through BackStack through a for loop, and when I have a match beetween my fragments tag and a fragment in BackStack, I delete the stack. I think this stack is a fragmentTransaction so I wondered if it was possible to delete a whole Fragment Tansaction and how to do that :)

private void setFragment(Fragment fragment) {
        FragmentManager manager = getSupportFragmentManager();
        String tagFromFragAddedToBackStack = getCurrentFragment().getTag();
        String fragmentTag = fragment.getClass().getName();
        String fragFromBackStackTag;
        FragmentTransaction fragTransFromBackStack;
        int i;

        for ( i = 0; i < manager.getBackStackEntryCount() ; i++){

                fragFromBackStackTag = manager.getBackStackEntryAt(i).getName();
                fragTransFromBackStack = (FragmentTransaction) manager.getBackStackEntryAt(i);
                fragFromBackStack = manager.findFragmentByTag(fragFromBackStackTag);

                if (fragFromBackStackTag == fragmentTag){

              // Here, I would like to delete the stack which matches 

                }
            }
            FragmentTransaction ft = manager.beginTransaction();
            ft.replace(R.id.main_frame, fragment, fragmentTag);
            ft.addToBackStack(tagFromFragAddedToBackStack); 
            ft.commit();
    }

  private Fragment getCurrentFragment(){
        if (homeFragment.isVisible()){
            return homeFragment;
        }
        else if (autonomyFragment.isVisible()) {
            return autonomyFragment;
        }
        else if (shopFragment.isVisible()) {
            return shopFragment;
        }
        else if (accountFragment.isVisible()) {
            return accountFragment;
        }
        else
            return null;
    }

So I'd expected to have a solution to my global problem which is: when the user clicks sequentially on back button, he doesn't go more than one time on each fragment Thanks :)

mathislr
  • 3
  • 3
  • While this is not the official solution (hence a comment not an answer), you could try using my library [simple-stack](https://github.com/Zhuinden/simple-stack) (see the [rationale](https://medium.com/@Zhuinden/simplified-fragment-navigation-using-a-custom-backstack-552e06961257) behind it), because I have a `moveToTop()` method that would let you reorder your backstack when moving from `[A,B,C]` to `B` and resulting in `[A,C,B]` for example. But you can use `setHistory` to move to anywhere you want in any order. Easier than hacking the symmetry of the FragmentManager backstack, imo. – EpicPandaForce Jul 24 '19 at 14:49
  • @EpicPandaForce thank you very much for your comment. As I said, I'm a real beginner, can you tell me how I can implement your library in my code (if it's too long, could you just pass me a link) :) – mathislr Jul 24 '19 at 15:19
  • No prob, you have to [add the integration to handle back + to let the library know what you're using to navigate (in this case fragments)](https://github.com/Zhuinden/simple-stack/blob/22e076b6eff18b539d9dd9991c822c35e09d18bd/simple-stack-example-basic-java-fragment/src/main/java/com/zhuinden/navigationexamplefrag/application/MainActivity.java#L59-L81), and for Fragment-based navigation you need to take the contents in the `core/navigation` package of that sample (the link was too long for this comment). If you have those in place, then you can use `setHistory` like in the sample. – EpicPandaForce Jul 24 '19 at 15:49

0 Answers0