Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

android - Returning to DialogFragment from another Activity reuses my enter animation

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

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:

<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:

<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>

Here's the code for creating the DialogFragment:

https://pastebin.com/k1c6nz3p

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
 }

By doing it in onPause() method you are disable all window animations without also to freeze any touch events.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...