正确的方法来删除DialogFragment:驳回()或transaction.remove()? [英] Correct way to remove a DialogFragment: dismiss() or transaction.remove()?

查看:356
本文介绍了正确的方法来删除DialogFragment:驳回()或transaction.remove()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然有一个问题,由于 DialogFragment 在我的主要活动使用。

I still have a problem due to a DialogFragment used on my main activity.

我目前使用这种code将其删除:

I am currently using this code to remove it:

FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fragment frag = getFragmentManager().findFragmentByTag("LockDialog");

if(frag != null) {
    transaction.remove(frag);
    transaction.commit();
}

问题是,我仍然得到崩溃,由于该对话框中有重复的事实(即没有正确有时删除对话框)。

The problem is that I am still getting crashes due to the fact that the dialog has duplicates (meaning the dialog was not properly removed sometimes).

所以我的问题是:是不是删除 DialogFragment 正确的方式还是必须仅只能用于片段

So my question is: is it a right way to remove a DialogFragment or must it be only used only for Fragments?

我必须使用辞退()方法,所有的时间:

Do I have to use the dismiss() method all the time?:

Fragment lockFragment = getFragmentManager().findFragmentByTag("LockDialog");

//If the dialog already exist, we dismiss it
if(lockFragment != null  && lockFragment instanceof LockDialogFragment) {
    LockDialogFragment lockDialog = (LockDialogFragment) lockFragment;
    lockDialog.dismiss();
}

这是目前我的,所以我想确保我的应用程序的一个最大的错误选择一个或另一个之前。

This is currently my biggest bug on one of my apps so I want to be sure before choosing one or the other.

谢谢!

修改:我才意识到我现在的问题是,可能是由于,承诺可以推迟,我会添加一个事实 executePendingTransactions 来看看它变得更好。
但它仍然带来了另外一个问题,是否需要调用transaction.remove()如果对话已被解雇?使用辞退()比使用交易更加简单和安全的?

EDIT: I just realized my current problem is maybe due to the fact that commits can be delayed, I will add executePendingTransactions to see if it gets any better. But still it brings another question, is it necessary to call transaction.remove() if the dialog has been dismissed? Is using dismiss() more straightforward and safe than using the transactions?

推荐答案

DialogFragment.dismiss()是正确的方法。从文档

关闭的碎片,它的对话框。如果上述DNA片段加入到
  回堆,都回来了堆栈状态直至及包括此项目将
  被弹出。否则,一个新的事务将致力于去除
  片段。

Dismiss the fragment and its dialog. If the fragment was added to the back stack, all back stack state up to and including this entry will be popped. Otherwise, a new transaction will be committed to remove the fragment.

这篇关于正确的方法来删除DialogFragment:驳回()或transaction.remove()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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