如何在不返回堆栈中的最后一项的情况下解除DialogFragment

时间:2014-09-01 04:11:03

标签: android dialogfragment

在尝试清理现有项目中的代码并删除有关在Activity中调用showDialog()的警告时,我已经移动了一堆相关的对话框,这些对话框按序列显示在新的DialogFragment类中。它工作正常,但是当我按下给定对话框上的取消按钮时,它总是将我带回到上一个对话框(已保存到后台堆栈),而我希望取消按钮解除所有对话框并返回主活动中的视图。

按后退按钮应该继续返回到后堆栈上的上一个对话框。

这是我当前的代码,我已将其简化为仅包含两个对话框,但在这两个对话框之间还有几个对话框,这些对话框在调用import()之前显示在我的实际应用程序中。

public class ImportDialog extends DialogFragment {
    private int mType = 0;

    public static final int DIALOG_IMPORT_HINT = 0;
    // ... several more
    public static final int DIALOG_IMPORT = 8;



    public interface ImportDialogListener {
        public void import();
        public void showImportDialog(int type);
        public void dismissAllDialogFragments();
    }


    /**
     * A set of dialogs which deal with importing a file
     * 
     * @param dialogType An integer which specifies which of the sub-dialogs to show
     */
    public static ImportDialog newInstance(int dialogType) {
        ImportDialog f = new ImportDialog();
        Bundle args = new Bundle();
        args.putInt("dialogType", dialogType);
        f.setArguments(args);
        return f;
    }


    @Override
    public AlertDialog onCreateDialog(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mType = getArguments().getInt("dialogType");
        Resources res = getResources();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        setCancelable(true);

        switch (mType) {
            case DIALOG_IMPORT_HINT:
                // First dialog
                builder.setTitle("Title");
                builder.setMessage("Message");
                builder.setPositiveButton(res.getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ((ImportDialogListener) getActivity()).showImportDialog(DIALOG_IMPORT_SELECT);
                    }
                });
                builder.setNegativeButton(res.getString(R.string.dialog_cancel), mNegativeClickListener);
                return builder.create();

            // ... several more

            case DIALOG_IMPORT:
                // Second dialog
                builder.setTitle("Different title");
                builder.setMessage("Different message");
                builder.setPositiveButton(res.getString(R.string.import_message_add),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ((ImportDialogListener) getActivity()).import();
                            }
                        });

                builder.setNegativeButton(res.getString(R.string.dialog_cancel), mNegativeClickListener);
                builder.setCancelable(true);
                return builder.create();

            default:
                return null;
        }
    }

    private DialogInterface.OnClickListener mNegativeClickListener = new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            ((ImportDialogListener) getActivity()).dismissAllDialogFragments(); 
        }
    };
}

然后在主Activity中我按如下方式实现ImportDialogListener接口:

public void showImportDialog(int type) {
    // DialogFragment.show() will take care of adding the fragment
    // in a transaction.  We also want to remove any currently showing
    // dialog, so make our own transaction and take care of that here.
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    // save transaction to the back stack (input argument is optional name) so it can be reversed
    ft.addToBackStack(null);
    DialogFragment newFragment = ImportDialog.newInstance(type);
    newFragment.show(ft, "dialog");
}

public void import() {
    // Do stuff
}

// Dismiss whatever dialog is showing... THIS IS THE PART THAT DOESN'T WORK
public void dismissAllDialogFragments() {
    getSupportFragmentManager().popBackStack("dialog", FragmentManager.POP_BACK_STACK_INCLUSIVE);
    Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        DialogFragment df = (DialogFragment) prev;
        df.dismiss();
    }
}

我怎样才能让它发挥作用,我在这里做了一些根本错误的事情?

1 个答案:

答案 0 :(得分:4)

在向堆栈添加事务时,请提供后堆栈状态的名称。例如对话框

更改

ft.addToBackStack(null);

ft.addToBackStack("dialog");

要从后堆栈中删除与后堆栈状态名称匹配的条目,请使用FragmentManager.popBackStack。这将从顶部到达堆栈底部或者到达具有不同名称的后堆栈状态条目,从名称对话框中删除所有后堆栈状态。无需在对话框中明确调用dismiss。

public void dismissAllDialogFragments() {
    getSupportFragmentManager().popBackStack("dialog", FragmentManager.POP_BACK_STACK_INCLUSIVE);
}