如何清除所有片段动画,包括清除后台堆栈时的退出动画,目前我使用的是以下代码,但它似乎没有停止动画退出屏幕,而只是停止动画进入屏幕的新片段。
我正在使用的代码如下:
菜单片段 - 用于清除堆栈的代码
private void clearBackStack() {
FragmentUtils.sDisableFragmentAnimations = true;
getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentUtils.sDisableFragmentAnimations = false;
}
Fragment Utils类:
public class FragmentUtils {
public static boolean sDisableFragmentAnimations = false;
}
所有片段都有:
@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);
}
导航菜单代码(我要禁用动画的地方)
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_events:
replaceFragment(new MainFragment());
break;
case R.id.action_promotions:
replaceFragment(new MainFragment());
break;
case R.id.action_menu:
clearBackStack();
replaceFragment(new MenuFragment());
break;
}
return true;
}
});
答案 0 :(得分:0)
public void replaceFragment(BaseFragment fragment, int resId, boolean addToBackStack) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(0, 0);
transaction.replace(resId, fragment);
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commitAllowingStateLoss();
}