Android Support Library (v4) と ActionBarSherlock を使用しています。プログラムで進行状況ダイアログを閉じようとしています。ダイアログ管理に役立つ小さなユーティリティ クラスをコーディングしました。
ダイアログは から表示されますAsyncTask.onPreExecute
。正しく表示されます。次に、デバイスを回転させて構成の変更を開始します。これにより、アクティビティが破棄されます (onDestroy 呼び出しAsyncTask.cancel(true)
)。AsyncTask.onCancelled
が呼び出され、ダイアログを閉じようとしているこのメソッドにあります。しかし、何も起こりません。ダイアログを表示して閉じるためのヘルパー関数は次のとおりです。
public abstract class DialogHelperActivity extends SherlockFragmentActivity {
protected void showProgressDialog(final String msg, final String tag){
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
DialogFragment dialogFragment = ProgressDialogFragment.newInstance(msg);
ft.add(dialogFragment, tag);
ft.disallowAddToBackStack();
ft.commitAllowingStateLoss(); //If I try with regular commit(), exceptions are thrown.
}
protected void closeDialog(final String tag){
FragmentManager fm = this.getSupportFragmentManager();
Fragment dialogFragment = fm.findFragmentByTag(tag);
if(dialogFragment != null){
FragmentTransaction ft = fm.beginTransaction();
ft.remove(dialogFragment);
ft.commitAllowingStateLoss();
} else {
System.err.println("dialog not found!"); //This line is hit always
}
}
public static class ProgressDialogFragment extends SherlockDialogFragment {
static ProgressDialogFragment newInstance(final String msg) {
ProgressDialogFragment adf = new ProgressDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("alert-message", msg);
adf.setArguments(bundle);
return adf;
}
@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();
String message = bundle.getString("alert-message");
ProgressDialog dialog = new ProgressDialog(getActivity());
if(message != null){
dialog.setMessage(message);
}
dialog.setCancelable(false);
dialog.setIndeterminate(true);
return dialog;
}
}
}
デバイスをローテーションした後、AsyncTask はキャンセルされます。と から電話closeDielog
しonPostExecute
ていonCancelled
ます。タグ ID が見つからない ( findFragmentByTag
null を返す) ため、ダイアログは閉じられません。私はこれに困惑しています。showProgressDialog
このタグは、私の実装アクティビティでは静的文字列であるため、との呼び出し間でタグが失われたり変更されたりする可能性はありませんcloseDialog
。
どんなアイデア/ヒント/提案も大歓迎です。
ありがとう。