- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在 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/
在 android 中提交带有状态丢失的 dialogfragment 事务有什么影响:因为它只是一个简单的错误对话框,我显示了一个 ok 按钮来关闭它,我认为我不需要担心状态丢失。 在我的 Dial
我是一名优秀的程序员,十分优秀!