'How to disable DialogFragment transition animation when resuming the application?

I have the following logic that sets a custom animation for my DialogFragment to slide in and out from the right hand screen in Kotlin.

dialog?.window?.attributes?.windowAnimations = R.style.dialog_style

Everything is great, but when I go to my mobile phone's home screen and effectively placing my app into the background and then when I bring it back I see that the animation activates as well.

Is there a way to temporarily disable the animation whenever I reopen the application from the background and then restore the animation when the DialogFragment loads.

It looks weird in my opinion to see the DialogFragment transition in when I reopen the app, it looks better when there is no animation (default style)

I want the custom animation to apply only when I'm navigating between fragments in my app, but no animation when the fragment is resumed from a background state (paused?).



Solution 1:[1]

I used savedInstanceState to check if the app is being reopened and set windowEnterAnimation to null. Here's my solution.

First, create two styles.

<style name="Animation.MyApp.CustomDialogFragment">
    <item name="android:windowEnterAnimation">@anim/slide_in</item>
    <item name="android:windowExitAnimation">@anim/slide_out</item>
</style>

<style name="Animation.MyApp.CustomDialogFragment.Restore">
    <item name="android:windowEnterAnimation">@null</item>
</style>

Then, override onCreateDialog() from DialogFragment.

open class CustomDialogFragment : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)

        if (savedInstanceState == null) {
            dialog.window?.setWindowAnimations(
                R.style.Animation_MyApp_CustomDialogFragment
            )
        } else {
            dialog.window?.setWindowAnimations(
                R.style.Animation_MyApp_CustomDialogFragment_Restore
            )
        }

        return dialog
    }
}

Solution 2:[2]

  1. Create your styles
<style name="Animation.App.FlushMessage" parent="@android:style/Animation.Activity">
        <item name="android:windowEnterAnimation">@anim/anim_flush_message_in</item>
        <item name="android:windowExitAnimation">@anim/anim_flush_message_out</item>
    </style>

    <style name="Animation.App.FlushMessage.Exit" parent="@android:style/Animation.Activity">
        <item name="android:windowEnterAnimation">@null</item>
        <item name="android:windowExitAnimation">@anim/anim_flush_message_out</item>
    </style>
  1. Override onStart:
class YourDialogFragment: DialogFragment() {

  var setupAnim = false

  override fun onStart() {
    super.onStart()
    
    if (setupAnim) {
        requireDialog().window?.setWindowAnimations(R.style.Animation_App_FlushMessage_Exit)
    } else {
        setupAnim = true
        requireDialog().window?.setWindowAnimations(R.style.Animation_App_FlushMessage)
    }
  }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yosemiteyss
Solution 2 Petrus Nguyễn Thái Học