问题描述
我推了碎片堆碎片使用以下code:
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();
此方式,当片段栈被弹出,如由pressing后退按钮,一个片段弹出播放动画。然而,有些情况下,我想弹出片段backstack而不显示此动画,如因为我刚刚从另一项活动回来,要显示的previous片段一次,没有动画。
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:
- 在用户的开始屏幕的根片段 在
- 他选择的根片段然后显示一个新的片段,表明项目的详细信息的一个项目。它使用一个特定片段的事务,设置动画,无论是推,并在弹出的情况下(这样,当用户presses后退按钮时,过渡动画)
- 从这个片段,他开始它(无论何种原因)将删除刚刚显示的项目活动
- 当本次活动结束后,我想返回到根片段,而不显示弹出动画的细节片段的
有没有办法弹出片段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);
}
然后,当你需要清除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.
这篇关于弹出的片段backstack不播放流行音乐,动漫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!