方向更改后,DialogFragment未正确附加

时间:2014-09-26 15:11:18

标签: android dialogfragment

我的Android应用程序中有以下结构:

[Main FragmentActivity] - > [DialogFragment#1] - > [DialogFragment#2] - > [DialogFragment#3(日历/日期选择器)]

现在,当我在 DialogFragment#3 中选择日期,并且我旋转设备并恢复之前的状态时, DialogFragment#3 不是不再附加到 DialogFragment#2 ,而是附加到主要活动(Main FragmentActivity)。

为什么会发生这种情况?如何防止此行为?

编辑1

我正在使用支持库。

此处的每个请求是用于显示片段的代码。

显示 DialogFragment#1

String tag = classDialog_1.class.getCanonicalName();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag(tag);
if(prev != null)
    fragmentTransaction.remove(prev);
fragmentTransaction.addToBackStack(null);
classDialog_1 instanceClassDialog_1 = classDialog_1.newInstance();
instanceClassDialog_1.show(fragmentTransaction, tag);

显示 DialogFragment#2

String tag = classDialog_2.class.getCanonicalName();
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
Fragment prev = getActivity().getSupportFragmentManager().findFragmentByTag(tag);
if(prev != null)
    fragmentTransaction.remove(prev);
fragmentTransaction.addToBackStack(null);

classDialog_1 instanceClassDialog_2 = classDialog_2.newInstance(paramA, paramB);
instanceClassDialog_2.show(fragmentTransaction, tag);

显示 DialogFragment#3

String tag = this.getClass().getName() + classDialog_3.class.getCanonicalName();
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
Fragment prev = getActivity().getSupportFragmentManager().findFragmentByTag(tag);
if (prev != null)
    fragmentTransaction.remove(prev);
fragmentTransaction.addToBackStack(null);

classDialog_3 instanceClassDialog_3 = classDialog_3.newInstance(paramC, paramD, paramE);
instanceClassDialog_3.show(fragmentTransaction, tag);

编辑2

正如下面的答案所示,代码修改如下但不起作用(例如 DialogFragment#3 ):

String tag = this.getClass().getName() + classDialog_3.class.getCanonicalName();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag(tag);
/* This section wasn't removed since without it the dialog ( instanceClassDialog_3 ) wasn't showing up. */
if (prev != null)
    fragmentTransaction.remove(prev);
fragmentTransaction.addToBackStack(null);

classDialog_3 instanceClassDialog_3 = classDialog_3.newInstance(paramC, paramD, paramE);
instanceClassDialog_3.show(fragmentTransaction, tag);

1 个答案:

答案 0 :(得分:0)

因此,查看您的代码时,DialogFragment只能附加到Activity。他们永远不会作为孩子Fragment附上。

首先,在再次显示之前,你真的不需要删除之前的DialogFragment实例--Android会在配置更改时保持它,所以你可以检查:

if (getSupportFragmentManager().findFragmentByTag(tag) != null) {
    // Show your DialogFragment
    ClassDialog1 dialog = ClassDialog1.newInstance();
    dialog.show(getSupportFragmentManager(), tag);
} else {
    // DialogFragment is already showing
}

现在,根据我的理解,您的问题是您希望其他DialogFragment成为Fragment的{​​{1}}个孩子?如果是,则需要使用ClassDialog1' Fragment - 而不是FragmentManager

如果你在Activity班级:

Activity

如果你在// This is the Activity's FragmentManager getSupportFragmentManager(); 班级:

Fragment

如果您希望将// This is the Activity's FragmentManager getActivity().getSupportFragmentManager(); // This is the Fragment's FragmentManager getFragmentManager(); 附加到其他Fragment,则需要使用托管Fragment' Fragment而不是FragmentManager ' S