DialogFragment不解散 [英] DialogFragment not dismissing

查看:137
本文介绍了DialogFragment不解散的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,一个解决方案,但似乎这不是一个普遍的问题。

I've looked around for a solution but it seems this is not a common problem.

我想在我的应用程序连接到服务器,然后明确表示,对话的请求完成时产生不确定的对话和微调显示不同的对话框。我使用了片段兼容包。问题是,在显示第2对话之前喷丝没有被删除。

I want to have an indeterminate dialog spinner while my app connects to a server then clear that dialog and display a different dialog when the request completes. I'm using the Fragment compatibility package. The problem is that the spinner is not being removed before the 2nd dialog is displayed.

下面是我的code,显示对话框,并应删除任何当前的对话框:

Here is my code that shows the dialogs, and should remove any current dialogs:

void displayDialog(int type, String message) {
    Log.i(logTag, "displayDialog: " + type);
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        Log.i(logTag, "removing previous dialog");
        ft.remove(prev); //TODO maybe use ((DialogFragment)dialog).dismiss(); ?
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    DialogFragment newFragment = DialogHelperFragment.newInstance(type, message);
    newFragment.show(ft, "dialog");
}

下面是调用code,我使用来解决此错误:

Here is the calling code that I'm using to troubleshoot this bug:

displayDialog(DialogHelperFragment.DIALOG_PROGRESS, null);
displayDialog(DialogHelperFragment.DIALOG_PURCHASE_SUCCESS, null);

下面是我相应的LogCat中输出:

Here's my corresponding LogCat output :

06-25 13:53:35.497: I/tag(11008): displayDialog: 8
06-25 13:53:35.497: I/tag(11008): displayDialog: 7
06-25 13:53:35.897: I/tag Dialog Helper(11008): Creating Dialog: 8
06-25 13:53:35.907: I/tag Dialog Helper(11008): Creating Dialog: 7

问题在于

Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");

由于第一个对话框尚未建立或时间displayDialog()附再次调用返回null。

returns null because the first dialog hasn't been created or attached by the time displayDialog() is called again.

任何提示将是超级有帮助的。

Any tips would be super helpful.

推荐答案

我一直在寻找的是

getSupportFragmentManager().executePendingTransactions()

这里。看来交易无需急于去。此调用赶紧通过事务。我的交易的顺序是现在如下:

as shown here. It seems the transaction wasn't in a hurry to go through. This call hurries the transaction through. The ordering of my transactions is as follows now:

06-26 10:45:43.800: I/tag(3303): displayDialog: 8
06-26 10:45:43.800: I/tag(3303): Previous Dialog Fragment is:null
06-26 10:45:43.810: I/tag(3303): displayDialog: 7
06-26 10:45:43.810: I/tag(3303): Previous Dialog Fragment is:DialogHelperFragment{40b44a78 #0 dialogHelp}
06-26 10:45:43.810: I/tag(3303): removing previous dialog
06-26 10:45:44.220: I/tag Dialog Helper(3303): Creating Dialog: 7

所以实际创建之前对话框中,键入8被删除。

so Dialog type 8 is removed before it is actually created.

我希望这有助于为那些谁卡住同样的问题。

I hope this helps for those who get stuck on the same problem.

修改

看来我也不得不删除 addToBackStack(空)

这篇关于DialogFragment不解散的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆