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?
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.
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.
If a single FragmentManager cannot support multiple independent stacks, then the solution is to have multiple FragmentMangers; we’re about to go deeper.
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.
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.
Here is complete of this project on github
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With