Adrime Adrime - 11 months ago 58
Android Question

Actions in onActivityResult and "Error Can not perform this action after onSaveInstanceState"

Implementing an app where the user can log in I have the following situation: If the user is logged in perform the action else start the login activity for result and if the result is Activity.RESULT_OK do the action.

My problem is that the action to perfom is to show a DialogFragment, but calling

DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);, "dialog")

in the onActivityResult callback throws an exception:

Caused by: java.lang.IllegalStateException:
Can not perform this action after onSaveInstanceState

So how can I solve this? I'm thinking in raising a flag there and show the dialog in the onResume but I see this solution a little dirty

Edit: Added more code (Im following this example for showing the DialogFragment

When the action is requested by the user:

if (!user.isLogged()){
startActivityForResult(new Intent(cnt, Login.class), REQUEST_LOGIN_FOR_COMMENT);

In the same fragment

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_LOGIN_FOR_COMMENT && resultCode == Activity.RESULT_OK) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = MyDialogFragment.newInstance();, "dialog")

And if the user logs in the Login activity calls;


Answer Source

Best thing I've come up with is to not use .show() but rather do this.

CheckinSuccessDialog dialog = new CheckinSuccessDialog();
//, null);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(dialog, null);