enter/exit和popEnter/popExit的区别是什么,以及它将在pop堆栈中运行的事务动画

lan*_*nyf 13 android fragmenttransaction fragment-backstack

在setCustomAnimations()中,动画需要四个资源ID.不是很了解他们.如果有人有更清晰的图片,如果你能解释,将不胜感激.

假设片段A添加在占位符和后台堆栈中.

FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.holder, fragA, FragmentA.FRAGMENT_NAME);
        ft.addToBackStack(FragmentA.FRAGMENT_NAME);
        ft.setCustomAnimations(R.anim.slide_in_from_bottom, R.anim.slide_in_from_top, R.anim.slide_in_from_left, R.anim.slide_in_from_right);
        ft.show(frag);
        ft.commit();
Run Code Online (Sandbox Code Playgroud)

并用片段B替换:

FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.holder, fragB, FragmentB.FRAGMENT_NAME);
        ft.addToBackStack(FragmentB.FRAGMENT_NAME);
        ft.setCustomAnimations(R.anim.slide_in_from_bottom, R.anim.slide_in_from_top, R.anim.slide_in_from_left, R.anim.slide_in_from_right);
        ft.show(frag);
        ft.commit();
Run Code Online (Sandbox Code Playgroud)

下次如果做一个popstack()

fm.popBackStackImmediate(FragmentB.FRAGMENT_NAME,
                                FragmentManager.POP_BACK_STACK_INCLUSIVE);
Run Code Online (Sandbox Code Playgroud)

它将运行哪个事务的动画?

/**
 * Set specific animation resources to run for the fragments that are
 * entering and exiting in this transaction. The <code>popEnter</code>
 * and <code>popExit</code> animations will be played for enter/exit
 * operations specifically when popping the back stack.
 */
public abstract FragmentTransaction setCustomAnimations(@AnimRes int enter,
        @AnimRes int exit, @AnimRes int popEnter, @AnimRes int popExit);
Run Code Online (Sandbox Code Playgroud)

kri*_*son 40

让我们开始简单的案例:

用片段B替换片段A(您的第二个代码片段)

  • 片段B运行输入动画
  • 片段A运行退出动画

按后退按钮并撤消更换操作

  • 片段B运行popExit动画
  • 片段A运行popEnter动画

现在回答你的问题.

您没有说容器是否已经有片段.让我们考虑两种情况:

  1. 当调用第一个用Fragment A替换的操作时,Container已经有了一个片段(让我们称之为片段0).弹出整个堆栈时:

    • 片段B运行popExit动画(在第二个片段中设置)
    • 片段0运行popEnter动画(在第一个片段中设置)
  2. 容器是空的,因此用Fragment A替换本质上是一个添加操作.弹出整个堆栈时:

    • 片段B运行popExit动画(在第二个片段中设置)
    • 由于容器现在为空,因此不会运行popEnter动画