android - 从后台返回时,BottomSheetDialogFragment 禁用动画

标签 android bottom-sheet

有一个扩展 BottomSheetDialogFragment 的类

当我将对话框设置为显示时,会出现底部动画的幻灯片并显示对话框。现在,如果我将应用程序设置为后台,然后将其带回前台,则已经显示的对话框会在动画中执行相同的幻灯片。

如何禁用此功能,即如果对话框已经显示,将应用程序发送到后台,然后前台不启动动画?

显示如下对话框:

dialog = MyDialogFragment()
dialog?.run {
    val args = Bundle()
    ....
    arguments = args
    show(this@MyFragment.fragmentManager, tag)
}

对话框类只有这个:
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 {
        ...
    }
}

这就是我对对话框的所有代码。

最佳答案

结合@nntk 和@CKotlin 答案对我来说是诀窍。我设法用这段代码解决了这个问题:

override fun onStop() {
    super.onStop()
    dialog?.window?.setWindowAnimations(-1)
}
编辑:
在 Android < P (API 28) 上,它使应用程序不响应触摸事件。所以要么你不使用它,要么你需要用支票包围它:
override fun onStop() {
    super.onStop()
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
        dialog?.window?.setWindowAnimations(-1)
    }
}

关于android - 从后台返回时,BottomSheetDialogFragment 禁用动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57110736/

相关文章:

android - 带有全屏工具栏和软键盘的模态 BottomSheetDialog

带有 RecyclerView 的 Android BottomSheet

android - 如何获取用户在谷歌地图中输入的地址的坐标?

Android代码 - 有什么方法可以将文本输入框覆盖为小写?

android - StrictMode.ThreadPolicy.Builder 目的和优势?

Android 持久 Bottom Sheet 初始可见性

Android BottomSheet 类似于 Share Sheet,底部有锚定 View

android - 是否有任何类被认为是 Android 中的上下文,或者是否有一些特定的事情必须完成?

Android - 在图像/缩略图上覆盖播放按钮的最佳方式

android - 在滚动 Activity 中使用 CustomBottomSheetBehavior 会导致工具栏后面出现空白