enter/exit 和 popEnter/popExit 动画有什么区别?

What is the difference between enter/exit and popEnter/popExit animations?

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();

并用片段 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();

下次如果做 popstack()

fm.popBackStackImmediate(FragmentB.FRAGMENT_NAME,
                                FragmentManager.POP_BACK_STACK_INCLUSIVE);

它将运行来自哪个交易的动画?

/**
 * 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);

让我们开始简单的案例:

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

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

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

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

现在回答你的问题。

你不说容器是否已经有片段。让我们考虑这两种情况:

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

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

    • 片段 B 运行 popExit 动画(在第二个片段中设置)
    • 没有 popEnter 动画运行,因为容器现在是空的