如何正确地从dialogfragment启动dialogframent?

时间:2015-07-03 15:24:28

标签: android android-dialogfragment

我从对话框片段A

开始对话片段B.
CreateNewPlayListDialog newPlayListDialog = new CreateNewPlayListDialog();
                    Bundle b = new Bundle();
                    b.putInt("id", videoId);
                    newPlayListDialog.setArguments(b);
                    newPlayListDialog.show(getChildFragmentManager(), "dialog_new_playlist");

这是有效的。但是,firstDialogFragment未被取消 如果我试试

CreateNewPlayListDialog newPlayListDialog = new CreateNewPlayListDialog();
                    Bundle b = new Bundle();
                    b.putInt("id", videoId);
                    newPlayListDialog.setArguments(b);
                    newPlayListDialog.show(getChildFragmentManager(), "dialog_new_playlist");
                    PlayListChooserDialog.this.dismiss();

然后,对话片段B开始并被imidiatly取消,Dialog A仍然存在;

如何从对话框A启动对话框A,如对话框A将被取消?

1 个答案:

答案 0 :(得分:0)

片段解雇只是从堆栈中删除最后一个片段的包装。

请将最后两行换成:

PlayListChooserDialog.this.dismiss();
newPlayListDialog.show(getChildFragmentManager(), "dialog_new_playlist");

如果你得到异常(取决于代码的其余部分在做什么),那么最安全的方法是直接在你的活动中获取一个方法并从那里加载另一个片段,例如。

public final ApplicationActivity extends Activity {
    public void onDialog(String whichOne) {
        if (TextUtils.isEmpty(whichOne) {
            return;
        }
        if (whichOne.equals.PlayListDialog.NAME) {
            newPlayListDialog.show(getChildFragmentManager(), "dialog_new_playlist");
        }
        else if(...) {
            ...
        }
    }
}

public final PlayListDialog extends DialogFragment {
    public static final String NAME = PlayListDIalog.class.getSimpleName();
    ...
}

然后在代码中解除片段A

((ApplicationActivity) getActivity()).onDialog(PlayListDialog.NAME);
 PlayListChooserDialog.this.dismiss();