BottomSheetDialogFragment disable animation when returning from background

喜夏-厌秋 提交于 2021-02-07 09:49:52

问题


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(this@MyFragment.fragmentManager, 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.


回答1:


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



回答2:


overwrite onStop() method in BottomSheetDialogFragment

such as

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

}



回答3:


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.



来源:https://stackoverflow.com/questions/57110736/bottomsheetdialogfragment-disable-animation-when-returning-from-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!