gpt4 book ai didi

android - 如何安全地关闭 onstop() 中的 DialogFragment?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:07:41 24 4
gpt4 key购买 nike

我需要在 FragmentActivity 的 onStop() 中关闭 DialogFragment,如果它正在显示,这就是我所做的

if(mAlertDlg != null && mAlertDlg.getDialog() != null)
mAlertDlg.dismiss();

但我通常会遇到 IllegalStateException。所以请告诉我为什么该代码是错误的,以及在 onStop() 中关闭 DialogFragment 的正确方法是什么?谢谢。

最佳答案

您应该使用 dialogFragment.dismissAllowingStateLoss()。正如文档对 commitAllowingStateLoss() 所说:

"Like commit() but allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change unexpectedly on the user."

因此对于 dismissAllowingStateLoss() 是相同的方法。

关于android - 如何安全地关闭 onstop() 中的 DialogFragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16842088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com