如何驳回DialogFragment无堆叠回到最后一个项目项目、DialogFragment

2023-09-07 13:23:59 作者:等你说爱我

在试图清理code在现有的项目,并删除有关调用警告的ShowDialog()在活动中,我提出了一堆相关的对话框其中在一个序列到一个新的 DialogFragment 类露面。当我preSS取消一个给定的对话框按钮,它可是工作正常,它总是把我带回到了previous对话框(这是保存到后退堆栈),而我想取消按钮关闭< STRONG>对话框的所有并返回到主活动视图。

In trying to clean up code in an existing project and remove warnings about calling showDialog() in an Activity, I have moved a bunch of related dialogs which show up in a sequence into a new DialogFragment class. It's working OK, however when I press the cancel button on a given dialog, it always takes me back to the previous dialog (which was saved to the back stack), whereas I want the cancel button to dismiss all of the Dialogs and go back to the View in the main activity.

pressing后退按钮应该继续回到previous对话框后面堆栈中。

Pressing the back button should continue to go back to the previous dialog on the back stack.

下面是我目前的code,我已经简化它仅包含两个对话框,虽然有链中的几个对话框,在这两者之间,这显示在我的实际应用程序之前,进口()被调用。

Here is my current code, I have simplified it to include only two dialogs, though there are several more dialogs in the chain between these two, which show up in my actual application before import() is called.

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(); 
        }
    };
}

然后在主活动我实施 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();
    }
}

我怎样才能得到它的工作,而我在做某种根本性错误在这里?

How can I get it to work, and am I doing something fundamentally wrong here?

推荐答案

虽然增加了交易后退堆栈,为后面的堆栈状态提供名称。例如对话框

While adding a transaction to back stack, supply the name for the back stack state. e.g. dialog

修改

ft.addToBackStack(null);

ft.addToBackStack("dialog");

要删除从后堆栈匹配回栈州名项,使用 FragmentManager.popBackStack 。这将删除所有回栈状态与顶级名对话框,直到到达栈的底部,或者达到与不同名称的后面堆状态条目。无需显式调用解雇对话框。

To remove entries from back stack matching a back stack state name, use FragmentManager.popBackStack. This removes all back stack states with name dialog from top until bottom of stack is reached or a back stack state entry with different name is reached. No need to explicitly call dismiss on dialog.

public void dismissAllDialogFragments() {
    getSupportFragmentManager().popBackStack("dialog", FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
 
精彩推荐
图片推荐