10
  1. I have created an AppCompatActivity Opened fragment A->B->C->D->E->F with replace()
  2. I am on F which contain button when I press the button I want to clear Fragments up to C and Want to open G on top of C so new Sequence will be A->B->C->G.I can do this with popBackStackImmediate() and add G on top of C with replace function.

Problem: When I press the button I see C for fraction of seconds and then G Is displayed on it.To Prevent this I tried to stop the animations with help of answer but C still visible for fraction of seconds even when the animation is stopped for fragments.

Is there any better way we can design fragment flow or way to solve this flicks when replacing fragment on top of C?

Uddhav P. Gautam
  • 7,362
  • 3
  • 47
  • 64
Kirtan
  • 1,782
  • 1
  • 13
  • 35
  • 1
    I have been trying to figure out that one myself lately. Disabling animation will - as I see it - only disable the transaction animations, but not the transactions themselves. – kazume Feb 22 '18 at 12:22
  • I think first replacing fragment and then clearing backstack would do the trick!! – Rushabh Shah Feb 26 '18 at 05:27
  • hey did you try the below answer, or still facing the issue? – 44kksharma Mar 02 '18 at 14:47
  • just my opinion: A,B,C in first Activity, then D,E,F,G in sedond Activity, when reach C u can popBackStackImmediate() if still need C or finish first Activity after start second Activity – Iqbal Rizky Mar 03 '18 at 08:35

2 Answers2

4

I was so curious about this question that i created a sample project and implemented the same use-case that you mentioned in your question. Here is how i handled this.

Used this method to remove F,E,D fragments from backstack

private void removeFragments() {
    getSupportFragmentManager().popBackStack("F", FragmentManager.POP_BACK_STACK_INCLUSIVE);
    getSupportFragmentManager().popBackStack("E", FragmentManager.POP_BACK_STACK_INCLUSIVE);
    getSupportFragmentManager().popBackStack("D", FragmentManager.POP_BACK_STACK_INCLUSIVE);
}

Used this method to replace fragment

private void replaceNewFragment(String key) {
    getSupportFragmentManager().beginTransaction().addToBackStack(key)
            .replace(android.R.id.content, AFragment.newInstance(key)).commit();
}

Here is a video demo video. enter image description here

Here is complete of this project on github

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
1

More Generic Solution for such navigation flow, Replace fragment like this

getSupportFragmentManager().beginTransaction().
                  replace(R.id.f_container,new FragmentA())
                    .addToBackStack("A")
                    .commit();
getSupportFragmentManager().beginTransaction().
                  replace(R.id.f_container,new FragmentB())
                    .addToBackStack("B")
                    .commit();

like wise do it till fragment F and lets assume you have submit button on F now inside onClick of submit button

Pop back stack till D with POP_BACK_STACK_INCLUSIVE flag like below and Add replace container with fragment G

getActivity().getSupportFragmentManager().popBackStack("D",
FragmentManager.POP_BACK_STACK_INCLUSIVE);
getSupportFragmentManager().beginTransaction().
                      replace(R.id.f_container,new FragmentG())
                        .addToBackStack("G")
                        .commit();

Now when you press back button you will see Fragment C

I hope this will help you, its working for me

44kksharma
  • 2,740
  • 27
  • 32
  • This works for me. The trick is to use popBackStack() and not popBackStackImmediate() and bundle everything in a single "commit", just like 44kksharma suggests. – kazume Mar 03 '18 at 10:39
  • Yes, the trick is popBackStack instead of popBackStackImmediate but the thing is when we use popBackStack it plays animation which looks weird if we have set slide animation as the pop animation of Fragment C when displaying Fragment G. – Kirtan Mar 05 '18 at 12:07