【问题标题】:Destroy DialogFragment on onCreateDialog()在 onCreateDialog() 上销毁 DialogFragment
【发布时间】:2013-07-24 06:47:39
【问题描述】:

我有一个初始化 Google plus 视图的对话框片段,有时这些视图会失败,所以我想在该对话框显示给用户之前将其终止。

如何结束对话框创建过程?从返回 Dialog 对象的 onCreateDialog 返回 null 会破坏程序。

【问题讨论】:

    标签: android android-dialogfragment


    【解决方案1】:

    如果您想在 onCreateDialog 中关闭 DialogFragment,您可以执行以下操作:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        setShowsDialog(false);
        dismiss();
        return null;
    }
    

    无需重写 onActivityCreated()。

    【讨论】:

    【解决方案2】:

    使用在OnCreateDialog() 之后调用的onActivityCreated() Fragment 回调解决了这个问题。我从onCreateDialog() 返回一个有效的对话框,但用dismiss 标记应该关闭该对话框。

    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if(dismiss) {
            this.dismiss();
        }
    }
    

    【讨论】:

    • 我设法通过在 onCreateDialog() 本身中调用 dismiss() 来做到这一点。
    • 有效,但已弃用:use onViewCreated(View, Bundle) for code touching the Fragment's view and onCreate(Bundle) for other initialization. To get a callback specifically when a Fragment activity's Activity.onCreate(Bundle) is called, register a androidx.lifecycle.LifecycleObserver on the Activity's Lifecycle in onAttach(Context), removing it when it receives the Lifecycle.State.CREATED callback.developer.android.com/reference/androidx/fragment/app/… 。我创建了一个更新的答案:stackoverflow.com/a/69141205/878126
    【解决方案3】:

    其他答案有点过时,一个对我不起作用(在那里写了我的 cmets),所以这是我认为更新和工作的答案:

    onCreateDialog 回调中,有你的逻辑来判断它何时成功。如果失败,则返回一些默认对话框(无论如何都不会使用),同时添加到生命周期的 onStart 回调以关闭 DialogFragment(使用 dismissdismissAllowingStateLoss):

    fun Lifecycle.runOnStarted(runnable: () -> Unit) {
        addObserver(object : DefaultLifecycleObserver {
            override fun onStart(owner: LifecycleOwner) {
                super.onStart(owner)
                runnable.invoke()
            }
        })
    }
    
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        //create the builder of the dialog, and then check if need to dismiss
        if (needToDismiss) {
            lifecycle.runOnStarted{
                dismissAllowingStateLoss()
            }
            return builder.create()
        }
    
    

    替代方案,使用 kotlin 协程,不使用我制作的辅助函数:

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        //create the builder of the dialog, and then check if need to dismiss
        if (needToDismiss) {
            lifecycleScope.launch {
                whenStarted {
                    dismissAllowingStateLoss()
                }
            }
            return builder.create()
        }
    

    或者你可以使用这个使用 kotlin 协程的辅助函数来使它更短:

    fun LifecycleOwner.runOnStarted(runnable: () -> Unit) {
        lifecycleScope.launch {
            whenStarted{
                runnable.invoke()
            }
        }
    }
    

    用法会和以前一样短:

    runOnStarted{
        dismissAllowingStateLoss()
    }
    

    请注意,我使用 onStart 回调而不是 onCreate。原因是在某些情况下,onStart 有效,而 onCreate 无效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-26
      • 2018-11-04
      • 2014-12-02
      • 1970-01-01
      相关资源
      最近更新 更多