单击“后退"按钮时无法禁用片段事务动画 [英] Cannot disable fragment transaction animation when back button is clicked

查看:102
本文介绍了单击“后退"按钮时无法禁用片段事务动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为片段交易添加了一个弹出进入和弹出退出动画.单击后退"按钮时,我不想播放此动画.有什么方法可以执行此操作?

I added a pop enter and pop exit animation for fragment transaction. When the back button is clicked,I dont want to play this animation.Is there any way to do this ?

推荐答案

解决方案很简单,请使用

The solution is simple, use setCustomAnimations(int, int) instead of setCustomAnimations (int, int, int, int)

弹出后堆栈时将不播放动画.希望对您有所帮助.

The animations will not be played while popping the back stack. Hope it helps.

更新

我已经编写了您需要使用的方法.请检查一下.这是一个例子,

I have written the method you need to use. Please check that. Here is an example,

transaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);

使用带有两个参数而不是四个参数的方法.前两个参数指定输入动画,后两个参数指定退出动画.希望你现在明白了.

Use the method with two parameters instead of four. The first two parameters specify the enter animation while the second two specify the exit animation. Hope you understand now.

更新2

我已针对您的情况更新了此答案.前一种方法只会在提交前禁用动画,对您没有用.因此,如果要在提交后禁用动画,则需要执行一些其他步骤.我会给你一个详细的答案.

I have updated this answer with respect to your scenario. The previous method will only disable the animation before commit and is not useful for you. So, if you want to disable the animations after commit, then you need to perform a few more steps. I will give you a detailed answer on this.

第1步

您需要创建一个静态布尔变量来检查何时启用和禁用片段动画,

You need to create a static boolean variable to check when to enable and disable fragment animations,

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

第2步

您需要覆盖要启用/禁用动画的每个片段的onCreateAnimation()方法,

You need to override the onCreateAnimation() method of every fragment you want to enable/disable animations,

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

第3步

每当需要返回没有动画的主片段时,只需调用此方法即可.

And whenever you need to return to your main fragment without animations, just call this method.

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

这将首先禁用动画,然后从后堆栈中弹出片段,然后再次打开动画.

This will disable the animations first, then pop the fragment from the back stack and turn on the animations again.

我自己尝试过此解决方案,并且考虑到您的特殊情况,它对我来说非常有用.

I have tried this solution myself and it works perfectly for me keeping in mind your special scenario.

这篇关于单击“后退"按钮时无法禁用片段事务动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆