I have the following AsyncTask in a Fragment (the example is simplified for showing clearly the error):
private class LoginTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// Do network login
return null;
}
@Override
protected void onPostExecute(Void result) {
FragmentActivity act = (FragmentActivity) getActivity();
if (act != null && isAdded()) {
act.getSupportFragmentManager().beginTransaction()
.add(new LoginDialogFragment(), "loginMessage").commit();
}
}
}
Once, leaving the app while the task was running I received an IllegalStateException: Can not perform this action after onSaveInstanceState
.
I suppose it is because I called it between the onSaveInstanceState of the activiy and the unattachment of the fragment from the activity (or because the activity was unattached after the getActivity() call and the add-fragment call.
So how can I avoid this error in the future? Thanks!