在Activity转到后台后尝试显示对话框时我得到了java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
,我需要向用户显示此消息,是否有任何方法可以在恢复活动时显示对话框。我的代码是这样的:
public class MyTask extends AsyncTask<String,String,String>{
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
@Override
protected void onPreExecute() {
progressDialog.setMessage(getString(R.string.dialog_progress_message));
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
// HTTP POST user action
// App goes to background (user press home button i.e.)
// Getting message from server
return message;
}
@Override
protected void onPostExecute(String message) {
progressDialog.dismiss();
DialogAlert dialogAlert = DialogAlert.newInstance(message);
dialogAlert.setCancelable(false);
//App crash here
dialogAlert.show(getSupportFragmentManager(), TAG);
}
}
我尝试使用AsyncTaskLoader
,但是当活动恢复时,对话框不显示。任何想法。
答案 0 :(得分:2)
尝试使用以下代码段,
替换
dialogAlert.show(getSupportFragmentManager(), TAG);
与
getSupportFragmentManager().beginTransaction().add(dialogAlert, "tag")
.commitAllowingStateLoss();