如何将进度对话框设置为在作为片段进行管理时不可取消?

时间:2011-09-07 17:44:49

标签: android android-fragments

我正在使用ProgressDialog作为Fragment进行管理。即使我将ProgressDialog设置为不可取消,BACK按钮仍然会从堆栈中删除Fragment。我的内心课看起来像这样:

public static class ProgressDialogFragment extends DialogFragment {

    private DialogStyle dialogStyle;

    public static ProgressDialogFragment newInstance(String title, String message) {
        ProgressDialogFragment fragment = new ProgressDialogFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("message", message);
        fragment.setArguments(args);

        return fragment;
    }

    public void setDialogStyle(DialogStyle dialogStyle) {
        this.dialogStyle = dialogStyle;
    }


    @Override
    public ProgressDialog onCreateDialog(Bundle savedInstanceState) {
        String title = getArguments().getString("title");
        String message = getArguments().getString("message");

        ProgressDialog progressDialog = new ProgressDialog(getActivity());
        progressDialog.setTitle(title);
        progressDialog.setMessage(message);

        if(dialogStyle!=null) {
            switch (dialogStyle) {
                case CANCELABLE:
                    progressDialog.setCancelable(true);
                    break;
                case NON_CANCELABLE:
                    progressDialog.setCancelable(false);
                    break;

            }
        } else {
            progressDialog.setCancelable(false);
        }

        progressDialog.show();

        return progressDialog;
    }
}

然后我公开的方法是:

public void showProgressDialog(String title, String message, DialogStyle dialogStyle) {
            Fragment prev = fragmentManager.findFragmentByTag("progress dialog");
            if(prev!=null) {
                ft.remove(prev);
            }
            ft.addToBackStack(null);

            DialogFragment newFragment = ProgressDialogFragment.newInstance(title, message);
            ((ProgressDialogFragment)newFragment).setDialogStyle(dialogStyle);
            newFragment.show(fragmentManager, "progress dialog");
        }

因此,显而易见的混淆是BACK按钮删除了ProgressDialog,因为它被管理为Fragment。那么如何才能使Dialog无法取消?

尝试类似的事情似乎很奇怪:

@Override
    public void onBackPressed() {
        if(fragmentManager.fragmentManager.findFragmentByTag("progress dialog")!=null) {

        }
    }

2 个答案:

答案 0 :(得分:37)

而不是ProgressDialog,为什么不在DialogFragment上尝试setCancelable(false)

答案 1 :(得分:10)

您还可以在setCancelable(false)

上使用ProgressDialog