Returning to DialogFragment from another Activity reuses my enter animation
Asked Answered
K

2

3

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://mcmap.net/q/1735343/-bottomsheetdialogfragment-disable-animation-when-returning-from-background

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

Kumquat answered 5/12, 2020 at 20:8 Comment(0)
K
5

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.

Kentkenta answered 14/12, 2020 at 10:26 Comment(6)
No way, there must be another solution to this! This will remove exit animation, as well.Bedroll
@Bedroll I know this is old, but my solution to get round this was to reapply the animation - the trick being to put this into a runnable in the post method of one of your views, so that it doesn't get called until after the layout is drawn.Esdras
@CharlieWalker good trick, I like it. It worked! Thanks! For other ones reading this, you can add ``` override fun onResume() { super.onResume() view?.post { dialog?.window?.setWindowAnimations(R.style.BottomDialogSlideAnimation) } } ``` to have exit animation when user press back.Bedroll
@Bedroll I have facing same issue. Is there any other solution found apart from this? As I do not want to apply setWindowAnimation.Cerium
@MohitTrivedi No, Charlie's solution worked for me.Bedroll
Note that waiting for a .post is not a fool-proof solution. On slower devices (ex. on my Android 9 tablet), animation is reapplied and only then window checks "I have returned to app, should I play animation? Ah yes, animations are reapplied so I should play them".Schnorrer
A
0

Recycling Views can always be problematic especially if you intend it to be slightly different in each scenario.

I would recommend that either you stop recycling this Dialog and instead create a specific Dialog for each of your different cases, or that you simply pass a flag into your Dialog defining what/when to use the animations.

Astrolabe answered 13/12, 2020 at 7:3 Comment(2)
what do you mean by recycling dialog? I have only created this dialog once in Activity A. I am returning to Activity A from Activity B.Kumquat
You return to Activity A, and the Dialog is still there? Perhaps I didn't quite understand the problem.Binghi

© 2022 - 2024 — McMap. All rights reserved.