开发者

How to set progress dialog to not cancelable when being managed as a fragment?

I'm using a ProgressDialog managed as a Fragment. Even if I set the ProgressDialog to be non-cancelable, the BACK button will still operate to remove that Fragment from the stack. My inner class look like this:

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

And then the method I expose is:

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

So the obvious confusion here is that the BACK button removes the ProgressDialog because it's being managed as a Fragment. So how can I make it so that the Dialog is not cancelable?

Seems strange to try so开发者_运维技巧mething like:

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

        }
    }


Instead of ProgressDialog, why don't you try setCancelable(false) on DialogFragment?


You can also use setCancelable(false) on ProgressDialog

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜