Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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); newFragment.show(ft, "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

@Override 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();         newFragment.show(ft, "dialog")     } } 

And if the user logs in the Login activity calls;

setResult(Activity.RESULT_OK); finish(); 
like image 364
Addev Avatar asked Aug 24 '12 07:08

Addev


People also ask

Can not perform this action after onSaveInstanceState in Android?

The main cost of the problem is the fragment transaction is commit() after onSaveInstanceState() is called. So in short, if you ever see this happens on your end, it's likely that some of your fragment transaction is called after onSaveInstanceState() . You'll need to then debug your flow see how that could occur.

What is the use of onActivityResult in Android?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.


2 Answers

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

CheckinSuccessDialog dialog = new CheckinSuccessDialog(); //dialog.show(getSupportFragmentManager(), null); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.add(dialog, null); ft.commitAllowingStateLoss(); 
like image 102
Nathan Schwermann Avatar answered Oct 21 '22 22:10

Nathan Schwermann


Here is the workaround that works fine for me.

private void alert(final String message) {     Handler handler = new Handler(Looper.getMainLooper());     handler.post(new Runnable() {         public void run() {             AlertDialogFragment alertDialogFragment = AlertDialogFragment.newInstance(message);             alertDialogFragment.show(getFragmentManager(), ALERT_DIALOG_FRAGMENT);         }     });         } 
like image 45
Cheok Yan Cheng Avatar answered Oct 21 '22 22:10

Cheok Yan Cheng