我需要从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中检索底层对话框?这样做的正确方法是什么? (如果重新创建对话框,我不想泄漏引用。)
提前致谢。
答案 0 :(得分:3)
这可以通过调用DialogFragment.getDialog()
来实现。 DialogFragment
可以像往常一样使用FragmentManager.findFragmentByTag
检索。