从另一个Activity返回DialogFragment会重复使用我的输入动画 [英] Returning to DialogFragment from another Activity reuses my enter animation
问题描述
我有一个创建DialogFragment的活动(A).在该DialogFragment中,我有一个创建新活动(B)的按钮.当我完成活动B时,它将显示活动A中的DialogFragment,并重用我设置的自定义动画.返回活动A时,如何防止DialogFragment重用该动画?
I have an Activity (A) that creates a DialogFragment. In that DialogFragment, I have a button which creates a new Activity (B). When I finish Activity B, it displays the DialogFragment from Activity A and it reuses that custom animation I set. How do I prevent my DialogFragment from reusing that animation when returning to Activity A?
此答案适用于某些设备,但是会冻结某些设备上的整个窗口(因此为检查版本)
This answer works for some devices, however it freezes the entire window on some (hence the check version)
@Override
public void onStop() {
super.onStop();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
getDialog().getWindow().setWindowAnimations(-1);
}
}
https://stackoverflow.com/a/64454784/11110509
这是我创建自定义DialogFragment输入/退出动画的方式:
This is how I am creating my custom DialogFragment enter/exit animation:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().getAttributes().windowAnimations = R.style.FragmentDialogAnim;
return dialog;
}
<style name="FragmentDialogAnim">
<item name="android:windowEnterAnimation">@anim/loginactivity_left_to_right</item>
<item name="android:windowExitAnimation">@anim/loginactivity_right_to_left</item>
</style>
loginactivity_left_to_right:
loginactivity_left_to_right:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700"/>
</set>
loginactivity_right_to_left:
loginactivity_right_to_left:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="-100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700" />
</set>
以下是创建DialogFragment的代码:
Here's the code for creating the DialogFragment:
推荐答案
您应该在DialogFragment的 onPause()方法中禁用动画,而不要在 onStop()方法中禁用动画.只需删除 onStop()方法中当前拥有的所有代码行,并使用以下代码行添加 onPause():
You should disable the animation in onPause() method of your DialogFragment instead of onStop() method. Just remove all the lines of code you currently have in onStop() method and add onPause() with the below lines of code:
@Override
public void onPause() {
super.onPause();
if(getDialog()!=null)
getDialog().getWindow().setWindowAnimations(-1);
}
通过在onPause()方法中执行此操作,您将禁用所有窗口动画而不冻结任何触摸事件.
By doing it in onPause() method you are disable all window animations without also to freeze any touch events.
这篇关于从另一个Activity返回DialogFragment会重复使用我的输入动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!