gpt4 book ai didi

android - 使用 commitAllowStateLoss 的 dialogfragment

转载 作者:搜寻专家 更新时间:2023-11-01 08:50:01 26 4
gpt4 key购买 nike

在 android 中提交带有状态丢失的 dialogfragment 事务有什么影响:因为它只是一个简单的错误对话框,我显示了一个 ok 按钮来关闭它,我认为我不需要担心状态丢失。

在我的 DialogFragment 子类中,我已经超越了 show 类,因此它 promise 包含状态丢失,这样我就不会得到 illegalstateException...

 @Override 
public void show(FragmentManager manager, String tag) {
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
//its just dialogs so can we allow state loss to not trigger illegalStateExceptions
ft.commitAllowingStateLoss();
}

最佳答案

根据这个Article最初,您试图避免此错误(原因:java.lang.IllegalStateException:在 onSaveInstanceState 之后无法执行此操作)。

此错误源于 onSaveInstanceState() 的这些 Bundle 对象表示调用 onSaveInstanceState() 时 Activity 的快照,仅此而已.这意味着当您在调用 onSaveInstanceState() 之后调用 FragmentTransaction#commit() 时,事务将不会被记住,因为它从未被记录为 Activity 状态的一部分第一名

您尝试使用 commitAllowingStateLoss() 来解决这个问题,让我们讨论一下调用commit()commitAllowingStateLoss() 的区别在于后者在状态丢失时不会抛出异常。通常你不想使用这种方法,因为它意味着有可能发生状态丢失。当然,更好的解决方案是编写您的应用程序,确保在保存 Activity 状态之前调用 commit(),因为这会带来更好的用户体验。除非无法避免状态丢失的可能性,否则不应使用 commitAllowingStateLoss()

关于android - 使用 commitAllowStateLoss 的 dialogfragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24432391/

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