Clear Android Fragment stack back without appearing?

We have an Activity that contains SlidingMenu ( https://github.com/jfeinstein10/SlidingMenu ), in which there are three options, call them A, B1, C1, They correspond to the fragments that we show in the Activity. When you select another option from SlidingMenu, I replace the current fragment with a new one using the FragmentManager.

From fragment B1 you can go to the other two, name them B2 and B3. Here we want the back key to take you from B2 β†’ B1 or from B3 β†’ B1, so I call transaction.addToBackStack (null). If we select an option from SlidingMenu when you are on B2 or B3, we want to clear the back stack, so I use the code suggested in this question to Clear the stack using fragments that popBackStack () calls until it is cleared.

So far so good.

From fragment C1, you can go to fragment C2. Since C1 / C2 have more Master / Detail design, I use

fragmentTransaction.setCustomAnimations(R.animator.slide_in_from_right, R.animator.slide_out_to_left, R.animator.slide_in_from_left, R.animator.slide_out_to_right); 

to add a slide animation where C1 slides to the left, as C2 slides to the right, and vice versa. Pressing the back key while turning on C2 brings us back to C1, with reverse animation, and all is well.

BUT

If you select A or B1 from SlidingMenu, and we popBackStack () to get rid of C1 from the back stack, it shifts C2 to the right, which looks strange. What I would like to do is clear the back stack without starting the animation, but I cannot find a way to do this. I tried calling popBackStackImmediate () instead, but that does not seem to make any difference.

As an alternative, I assume that I could not call addToBackStack at all, but instead manually process the user by pressing the Back key through Activity.onBackPressed (), but maybe there is a solution that I just don’t see?

+9
android android-fragments slidingmenu
Apr 26 '13 at 12:08
source share
2 answers

You can try following, although I am not familiar with .remove() , it seems that it should do what you want:

 myFragmentClass myFragC1 = (myFragmentClass) getFragmentManager().findFragmentByTag("theTagYouUsedWhenAddingToBackStack"); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.remove(myFragC1); transaction.commit(); 
+6
Apr 26 '13 at
source share

Unfortunately, I do not know how to do this correctly, so here is my solution (in fact, I completely changed another stackoverflow answer )

In the base fragment, which applies to all other fragments):

 public static boolean sDisableExitAnimation = false; @Override public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) { if (sDisableExitAnimation && !enter) { return AnimationUtils.loadAnimation(getActivity(), R.anim.clear_stack_exit); } return super.onCreateAnimation(transit, enter, nextAnim); } 

To clear the stack, you need to call:

 ScreenFragment.sDisableExitAnimation = true; manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 

And finally, the animation itself clear_stack_exit (I did not find a way to get it from the FragmentManager itself):

 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="1" android:toAlpha="0" android:duration="220" /> <scale android:fromXScale="1" android:toXScale="0.975" android:fromYScale="1" android:toYScale="0.975" android:pivotX="50%" android:pivotY="50%" android:duration="220" /> </set> 

This way you will have the correct clear animation of the stack. Just remember to clear the sDisableExitAnimation variable before starting the next jump.

+3
Aug 2 '13 at
source share



All Articles