当活动在后台时关闭 DialogFragment(支持库)会出现 IllegalStateException
在我的应用程序的活动中,我需要在上传某些内容时显示“正在加载”不可取消的 FragmentDialog。我从当前 Activity 中的回调中显示此对话框,如下所示:
final FragmentManager fm = getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
final Fragment prev = fm.findFragmentByTag( "loading" );
if ( prev != null ) {
ft.remove(prev);
}
ft.addToBackStack(null);
final DialogFragment df =
IndeterminateProgressDialogFragment.newInstance(
getString( R.string.loading_dialog_sending_data ) );
df.setCancelable( false );
df.show( ft, "loading" );
上传发生在从我的 ui 片段执行的保留片段(使用 asynctask)中。上传完成后,我的 ui 片段会在 onActivityResult 上收到通知,当前它会尝试关闭该对话框。在过去,它委托了通过 onActivityResult 上完成的回调来关闭加载对话框的责任,但它也不起作用。
当我的活动位于前台时,我可以关闭此对话框,但是当活动位于后台时,我会收到 IllegalStateException:在 onSaveInstaneState 之后无法执行此操作。我尝试使用 onRemove,这不会使我的应用程序崩溃,但也不会隐藏 DialogFragment。我已经在我的 ui 片段和活动上完成了这两件事,但仍然遇到问题。
我一直在阅读文档,但找不到可以使用的方法。
DialogFragment 不应该像我需要的那样使用吗?或者我应该做什么?
In an activity of my app I need to show a "Loading" uncancelable FragmentDialog while something is uploading. I show this dialog from a callback in my current Activity like this:
final FragmentManager fm = getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
final Fragment prev = fm.findFragmentByTag( "loading" );
if ( prev != null ) {
ft.remove(prev);
}
ft.addToBackStack(null);
final DialogFragment df =
IndeterminateProgressDialogFragment.newInstance(
getString( R.string.loading_dialog_sending_data ) );
df.setCancelable( false );
df.show( ft, "loading" );
The upload is happening in a retained Fragment (using asynctask) that is executed from my ui Fragment. When the upload is done, my ui Fragment is informed on onActivityResult and currently it tries to dismiss the dialog. In the past it delegates the responsability of dismissing the loading dialog with a callback done on onActivityResult, but it doesn't work too.
I can dismiss this dialog while my activity is on foreground, but when is on background I get an IllegalStateException: can not perform this action after onSaveInstaneState. I tried using onRemove and this does not make my app crash but also does not hide the DialogFragment. I've done both things on my ui Fragment and my activity and the still got the problem.
I have been reading the docs but I can´t find some method I can use.
Is DialogFragment not supposed to be used like I need or what should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我只是设法用一个肮脏的解决方法解决了这个问题:
我捕获了 IllegalStateException 并将标志设置为 true,所以当我的 Fragment 返回时我知道我试图解雇。
但我仍然想知道在 onBackground 时关闭 FragmentDialog 的正确方法是什么。我有另一个项目也有同样的问题:/
I just managed to solve this with a dirty workaround:
I catch the IllegalStateException and set a flag to true, so when my Fragment comes back I know I tried to dismiss.
But I'm still wondering what is the right way to dismiss a FragmentDialog while onBackground. I got another project with the same problem :/
您不需要在 onActivityResult 中关闭片段并捕获异常。
只需在 onActivityResult 中设置标记即可。我使用静态变量。
You don't need dismiss fragment in onActivityResult and catch exception.
Just set you flag in onActivityResult. I use static variable.