How to Reverse Fragment Animations on BackStack?

后端 未结 6 1476
日久生厌
日久生厌 2020-12-04 13:56

I thought the system would reverse animations on the backstack when the back button is pressed when using fragments using the following code:

FragmentManager         


        
6条回答
  •  情话喂你
    2020-12-04 14:25

    This is as mentioned in Fragment Transaction class .

    /**
         * Set specific animation resources to run for the fragments that are
         * entering and exiting in this transaction. The popEnter
         * and popExit animations will be played for enter/exit
         * operations specifically when popping the back stack.
         *
         * @param enter An animation or animator resource ID used for the enter animation on the
         *              view of the fragment being added or attached.
         * @param exit An animation or animator resource ID used for the exit animation on the
         *             view of the fragment being removed or detached.
         * @param popEnter An animation or animator resource ID used for the enter animation on the
         *                 view of the fragment being readded or reattached caused by
         *                 {@link FragmentManager#popBackStack()} or similar methods.
         * @param popExit An animation or animator resource ID used for the enter animation on the
         *                view of the fragment being removed or detached caused by
         *                {@link FragmentManager#popBackStack()} or similar methods.
         */
        @NonNull
        public abstract FragmentTransaction setCustomAnimations(@AnimatorRes @AnimRes int enter,
                @AnimatorRes @AnimRes int exit, @AnimatorRes @AnimRes int popEnter,
                @AnimatorRes @AnimRes int popExit);
    

    so finally you can use method like this

     mFragmentManager.beginTransaction()
                            .replace(R.id.container, fragment)
                            .setCustomAnimations(R.anim.slide_left,//enter
                                                 R.anim.slide_out_left,//exit
                                                 R.anim.slide_right,//popEnter
                                                 R.anim.slide_out_right)//popExit
                            .addToBackStack(fragment.toString())
                            .commit();
    

提交回复
热议问题