Favolas
Favolas

Reputation: 7243

BottomSheetDialogFragment disable animation when returning from background

Got a class that extends BottomSheetDialogFragment

When I set the dialog to show, the slide from bottom animation occurs and the dialog is shown. Now, If I set the app to background and then bring it back to the foreground, the dialog that was already showing does the same slide in animation.

How can I disable this, that is, if the dialog is already showing, sending the app to background and then foreground does not start the animation?

Showing the dialog like this:

dialog = MyDialogFragment()
dialog?.run {
    val args = Bundle()
    ....
    arguments = args
    show([email protected], tag)
}

Where the dialog class only has this:

class MyDialogFragment : BottomSheetDialogFragment() {

    var listener: Listener? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // inflate view
        // Given the arguments, sets up the ui

        return view
    }

    interface Listener {
        ...
    }
}

And that's all the code that I have for the dialog.

Upvotes: 6

Views: 6492

Answers (3)

punchlag
punchlag

Reputation: 258

Combining @nntk and @CKotlin answers made the trick for me. I manage to fix the issue with this code :

override fun onStop() {
    super.onStop()
    dialog?.window?.setWindowAnimations(-1)
}

Edit: On Android < P (API 28), it makes the app not responding to touch events. So either you don't use it or you need to surround it with a check:

override fun onStop() {
    super.onStop()
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
        dialog?.window?.setWindowAnimations(-1)
    }
}

Upvotes: 5

CKotlin
CKotlin

Reputation: 13

I'm dealing with the same issue. I haven't fully been able to solve this, but I was able to stop the slide up animation from occurring on resume by doing the following:

    override fun onResume() {
        super.onResume()
        // Disable dialog window animations for this instance
        dialog?.window?.setWindowAnimations(-1)
    }

This doesn't stop the background fade from animating though, so the background shade shows, disappears, and then shows again.

Upvotes: 0

nntk
nntk

Reputation: 7

overwrite onStop() method in BottomSheetDialogFragment

such as

@Override
public void onStop() {
    super.onStop();
    getDialog().show();  // important

}

Upvotes: 1

Related Questions