Manage Fragment Backstack Flow without flicks
Asked Answered
U

2

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?

Uranalysis answered 22/2, 2018 at 11:37 Comment(4)
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.Nessi
I think first replacing fragment and then clearing backstack would do the trick!!Modernistic
hey did you try the below answer, or still facing the issue?Paramedic
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 ActivityCirculate
G
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

Gulp answered 4/3, 2018 at 7:4 Comment(0)
P
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

Paramedic answered 1/3, 2018 at 10:30 Comment(2)
This works for me. The trick is to use popBackStack() and not popBackStackImmediate() and bundle everything in a single "commit", just like 44kksharma suggests.Nessi
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.Uranalysis

© 2022 - 2024 — McMap. All rights reserved.