如何更新ProgressDialog片段

时间:2012-11-05 17:30:59

标签: java android android-fragments progressdialog

我需要从AsyncTask内部更新进度对话框片段。我知道如何使用旧的ProgressDialog执行此操作,但现在这种显示对话框的方式已被弃用,我正在尝试对Fragments执行相同的操作。

这是片段代码:

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

这就是我展示它的方式:

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

现在我需要从AsyncTask通过对话框调用incrementProgressBy。有没有办法从FragmentManager中检索底层对话框?这样做的正确方法是什么? (如果重新创建对话框,我不想泄漏引用。)

提前致谢。

1 个答案:

答案 0 :(得分:3)

这可以通过调用DialogFragment.getDialog()来实现。 DialogFragment可以像往常一样使用FragmentManager.findFragmentByTag检索。