Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 something like:

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

        }
    }
like image 855
LuxuryMode Avatar asked Sep 07 '11 17:09

LuxuryMode


People also ask

How do I make progress dialog not cancelable?

So how can I make it so that the Dialog is not cancelable? Android seems to be saying that ProgressDialog should not be used anymore, that instead you should “use a ProgressBar in your layout.” developer.android.com/guide/topics/ui/dialogs.html.

How to show dialog Fragment in Activity?

Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

What is progress dialogue?

ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar , which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress.


2 Answers

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

like image 171
CommonMan Avatar answered Oct 04 '22 17:10

CommonMan


You can also use setCancelable(false) on ProgressDialog

like image 28
patrickfdsouza Avatar answered Oct 04 '22 18:10

patrickfdsouza