本文介绍了在不播放 Pop-Animation 的情况下弹出片段 backstack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码将一个片段推送到片段堆栈上:

I push a fragment on the fragment stack using the following code:

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right,
     R.anim.slide_in_left, R.anim.slide_out_left);
fragmentTransaction.replace(getId(), newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

这样,当片段栈被弹出时,例如通过按下后退按钮,播放片段弹出动画.但是,在某些情况下,我想在不显示此动画的情况下弹出片段返回堆栈,例如因为我刚从另一个 Activity 回来,想一次显示上一个片段,没有动画.

This way, when the fragment stack is popped, e.g. by pressing the back button, a fragment pop animation is played. However, there are situations in which i would like to pop the fragment backstack without showing this animation, e.g. because I just returned from another activity and want to display the previous fragment at once, without animation.

示例导航可能如下所示:

An example navigation could look like this:

  • 用户在带有根片段的开始屏幕上
  • 他在根片段上选择一个项目,然后显示一个新片段以显示该项目的详细信息.它使用一个片段事务来为推送和弹出案例设置动画(因此当用户按下后退按钮时,过渡是动画的)
  • 从这个片段开始,他开始一个活动(无论出于何种原因)删除刚刚显示的项目
  • 当这个活动结束时,我想回到根片段而不显示细节片段"的弹出动画"

有没有办法在不播放指定的pop动画的情况下弹出fragment backstack?

Is there a way to pop the fragment backstack without playing the specified pop animation?

推荐答案

所以 Warpzit 走在正确的轨道上,他只是没有很好地解决您的具体问题.我遇到了完全相同的问题,这是我的解决方法.

So Warpzit was on the right track, he just didn't address your specific issue too well. I came across the exact same issue and here is how I solved it.

首先我创建了一个静态布尔变量(为了简单起见,让我们把它放在 FragmentUtils 类中)...

First I created a static boolean variable (for simplicity's sake, lets put it in the FragmentUtils class)...

public class FragmentUtils {
    public static boolean sDisableFragmentAnimations = false;
}

然后,在您拥有的每个片段中,您都需要覆盖 onCreateAnimation 方法...

Then, in EVERY fragment you have, you need to override the onCreateAnimation method...

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    if (FragmentUtils.sDisableFragmentAnimations) {
        Animation a = new Animation() {};
        a.setDuration(0);
        return a;
    }
    return super.onCreateAnimation(transit, enter, nextAnim);
}

然后,当您需要从 Activity 中清除 backstack 时,只需执行以下操作...

Then, when you need to clear the backstack from your activity simply do the following...

public void clearBackStack() {
    FragmentUtils.sDisableFragmentAnimations = true;
    getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    FragmentUtils.sDisableFragmentAnimations = false;
}

瞧,调用 clearBackStack() 会将您带回到根片段中,而没有任何过渡动画.

And voila, a call to clearBackStack() will drop you back into the root fragment without any transition animations.

希望大 G 将来会添加一种不那么愚蠢的方法.

Hopefully the big G will add a less stupid way of doing this in the future.

这篇关于在不播放 Pop-Animation 的情况下弹出片段 backstack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:05