Android – “Failure Delivering Result ” – onActivityForResult

android, android-activity, android-fragmentactivity, android-intent

I have a LoginActivity (User Logs in). It is basically its own Activity that is themed like a dialog (to appear as if a dialog). It appears over a SherlockFragmentActivity. What I want is: If there is a successful login, there should be two FragmentTransaction's to update the view. Here is the code:

In LoginActivity, if successful login,

setResult(1, new Intent());

In SherlockFragmentActivity:

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if (resultCode == 1) {        LoggedStatus = PrefActivity.getUserLoggedInStatus(this);        FragmentTransaction t = MainFragmentActivity.this.getSupportFragmentManager().beginTransaction();        SherlockListFragment mFrag = new MasterFragment();        t.replace(, mFrag);        t.commit();        // Set up Main Screen        FragmentTransaction t2 = MainFragmentActivity.this.getSupportFragmentManager().beginTransaction();        SherlockListFragment mainFrag = new FeaturedFragment();        t2.replace(, mainFrag);        t2.commit();    }}

It crashes on the first commit, with this LogCat:

E/AndroidRuntime(32072): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceStateE/AndroidRuntime(32072):    at    at    at    at    at com.kickinglettuce.rate_this.MainFragmentActivity.onActivityResult(    at    at

Best Solution

First of all, you should read my blog post for more information (it talks about why this exception happens and what you can do to prevent it).

Calling commitAllowingStateLoss() is more of a hack than a fix. State loss is bad and should be avoided at all costs. At the time that onActivityResult() is called, the activity/fragment's state may not yet have been restored, and therefore any transactions that happen during this time will be lost as a result. This is a very important bug which must be addressed! (Note that the bug only happens when your Activity is coming back after having been killed by the system... which, depending on how much memory the device has, can sometimes be rare... so this sort of bug is not something that is very easy to catch while testing).

Try moving your transactions into onPostResume() instead (note that onPostResume() is always called after onResume() and onResume() is always called after onActivityResult()):

private boolean mReturningWithResult = false;@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    mReturningWithResult = true;}@Overrideprotected void onPostResume() {    super.onPostResume();    if (mReturningWithResult) {        // Commit your transactions here.    }    // Reset the boolean flag back to false for next time.    mReturningWithResult = false;}

This might seem a little weird, but doing this sort of thing is necessary to ensure that your FragmentTransactions are always committed after the Activity's state has been restored to its original state (onPostResume() is guaranteed to be called after the Activity's state has been restored).