Mister Smith
Mister Smith

Reputation: 28168

How to Update a ProgressDialog Fragment

I need to update a progress dialog fragment from inside an AsyncTask. I know how to do this using the old ProgressDialog, but now this way of showing dialogs is deprecated and I'm trying to do the same with Fragments.

This is the fragment code:

    public static class CustomProgressDialogFragment extends SherlockDialogFragment {

        static CustomProgressDialogFragment newInstance(Object... params){
            CustomProgressDialogFragment df = new CustomProgressDialogFragment();
            Bundle bundle = new Bundle();

            // Populate bundle with params (not shown)

            df.setArguments(bundle);
            return df;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.setCancelable(false);
            int style = DialogFragment.STYLE_NORMAL, theme = 0;
            setStyle(style,theme);
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            Bundle bundle = this.getArguments();

            ProgressDialog dialog = new ProgressDialog(getActivity());

            // Get params from bundle and customize dialog accordingly (not shown)

            dialog.setCancelable(false);
            dialog.setIndeterminate(false);
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

            return dialog;
        }
    }

and this is how I show it:

    FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();       
    CustomProgressDialogFragment dialogFragment = CustomProgressDialogFragment.newInstance(<My params here>);
    ft.disallowAddToBackStack();
    ft.add(dialogFragment, stringSet.tag);      
    ft.commitAllowingStateLoss();

Now I need to call incrementProgressBy over the dialog from the AsyncTask. Is there a way to retrieve the underlying dialog from the FragmentManager? What would be the proper way of doing this? (I don't want to leak references if the dialogs gets recreated).

Thanks in advance.

Upvotes: 2

Views: 1562

Answers (1)

Mister Smith
Mister Smith

Reputation: 28168

This can be achieved calling DialogFragment.getDialog(). The DialogFragment can be retrieved as usual, with FragmentManager.findFragmentByTag.

Upvotes: 3

Related Questions