Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage Fragment Backstack Flow without flicks

  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?

like image 301
Kirtan Avatar asked Feb 22 '18 11:02

Kirtan


People also ask

Is it possible to perform a popbackstack just before a fragment?

Others have tried to perform a popBackstack just before adding their own new fragment (causing a glitch on the monitor) The only solution i’ve found to work properly is to always add the transactions to the backstack and handle such “ A -> B -> C (back) -> A ” behavior by myself. For this reason i created a snippet that seems to work properly.

How do I implement a fragmenttransaction with fragmentmanger?

Implementing this with FragmentManger fortunately, is rather straightforward. When performing a FragmentTransaction, we can opt to add the Fragment to the FragmentManager’s back stack, so rather than trying to manage the stack yourself, and restore it across process death, you can delegate it all to the FragmentManager.

Can a fragmentmanager support multiple independent stacks?

If a single FragmentManager cannot support multiple independent stacks, then the solution is to have multiple FragmentMangers; we’re about to go deeper.

Should developers use back Press in fragments?

With such high-qua l ity development ideas, developers tend to move from traditional activities to effective fragments. This puts many developers in a tricky position (myself included in some cases) regarding the usage of back press in fragments.


1 Answers

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

like image 117
Zeeshan Shabbir Avatar answered Nov 16 '22 02:11

Zeeshan Shabbir