有时,在某些设备上,对话片段会因为上述非法状态异常而导致活动崩溃
我尝试过shownow(),它只在一些设备上有帮助,但问题仍然存在。
val dialog = CustomDialogFragment.newInstance(false, correctAnswer, true)
dialog.show(supportFragmentManager, "alert")我需要对话框实例以备将来使用,否则我会在newInstance()之后立即使用show。可能的解决方案是什么?
发布于 2019-11-27 17:58:48
当我发现这个问题是由Android问题引起的,我有以下解决方案:只需覆盖对话框片段的show()方法,如下所示:
@Override
public void show(@NonNull FragmentManager manager, @Nullable String tag) {
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
ft.commitAllowingStateLoss();
}发布于 2019-07-12 06:32:07
此问题的根本原因是您试图显示FragmentDialog活动已将其状态更改为onPause()。
要处理此问题,必须在显示对话框之前检查生命周期状态
if(lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)){
val dialog = CustomDialogFragment.newInstance(false, correctAnswer, true)
dialog.show(supportFragmentManager, "alert")
}https://stackoverflow.com/questions/56997901
复制相似问题