Take the 2-minute tour ×

I have 2 activities AAA and BBB. I call BBB from AAA using startActivityForResult(Intent, int). After I am done with BBB, I press the Back button to return to AAA. In BBB, I override onPause() and set the result by using setResult(RESULT_OK).

In AAA, I check my result in onActivityResult(int requestCode, int resultCode, Intent data) and I keep getting RESULT_CANCELLED.

After spending sometime on google/stackoverflow, I figured out that if I override onBackPressed() and set the result in it, then it works absolutely fine.

What I fail to understand is that, why is the result not getting set in onPause(), when in fact onPause() gets called after onBackPressed(). I have gone through the activity flows in the Dev docs and I am pretty clear about what has been mentioned there.

Anyone got any ideas about this behavior or could explain it better?

share|improve this question
Duplicate of stackoverflow.com/questions/2679250/… –  pjv Jun 9 '12 at 17:31
add comment

4 Answers

You should take a look at the onActivityResult reference. http://developer.android.com/reference/android/app/Activity.html#onActivityResult%28int,%20int,%20android.content.Intent%29

Called when an activity you launched exits, giving you the requestCode you started it with, the resultCode it returned, and any additional data from it. The resultCode will be RESULT_CANCELED if the activity explicitly returned that, didn't return any result, or crashed during its operation.

You will receive this call immediately before onResume() when your activity is re-starting.

Call setResult in finish(). Besause onPause() can be called when a new activity is start from BBB.

share|improve this answer
I had the option of setting the result by either overriding finish() or onBackPressed(). As far as the 3 options where it could be RESULT_CANCELLED, it is definitely not the crash option. It could be the second option. Don't know how to check that. Not sure if some other function sets the resultcode after I explicitly set it in onPause(). I also tried calling finish() in onPause(). –  Shubhayu Apr 5 '12 at 7:48
Why are you calling finish() in onPause(). onPause() called when it goes to background. If you press home button from BBB then onPause() will be called. And when onBackpressed() called finish() also called. That's why setResult() in onBackpressed() work. Why dont you put the result in those two method? –  Shaiful Apr 5 '12 at 8:12
I was trying out various things trying to figure out the flow and what was causing the behavior. I agree that onPause() will be called when Home button is clicked, but that still doesn't answer my question that why the result I set explicitly in onPause() is not propagated to AAA when I click the back button. –  Shubhayu Apr 5 '12 at 8:23
Ok. For figuring it out. Take a look at the source code of Activity –  Shaiful Apr 5 '12 at 11:28
add comment

I think the issue here might be that onPaused might be called after you have already returned to the previous activity. I saw similar behaviour in other testing. Try adding some Log.d printouts to confirm the order of the onPause call versus the onActivityResult call.

share|improve this answer
I checked that, also the flow of the activities is organized, so that is not the case developer.android.com/guide/topics/fundamentals/… –  Shubhayu Apr 5 '12 at 7:44
add comment

Its because onActivityResult(int requestCode, int resultCode, Intent data) not get called untill child Activity get finish()

You are setting result but you are not finishing your Activity BBB, thats why ActivityAAA's onActivityResult() not get called

When you override onBackPressed(), it finishes current activty on pressing back button, which definitely call onActivityResult() of Parent Activity

in fact onPause() gets called after onBackPressed()

onPause() pause the activity not finishes it, your result get sets in onPause() but call onActivityResult() of Parent, Child must be finished .

As you are setting result inonBackPressed(), and its finishes the activity too thats why its working fine.

share|improve this answer
I tried calling this.finish() in onPause() which was called after setting the result. Still didn't work. –  Shubhayu Apr 5 '12 at 7:35
but why you want to finish it in onPause()? –  hotveryspicy Apr 5 '12 at 8:28
my onActivityResult() gets called in AAA even when I don't call finish() but just set the result in onPause(). I called finish() to make sure that that finish() was getting called. –  Shubhayu Apr 5 '12 at 8:41
add comment

Most probably the case must be the life cycle methods.

When you press back key in BBB acctivty firstly onPause() is called so you set something in onPause() then this method is followed by onStop() and further follwed by OnDestroy() because the activity is being finished. So now if you set something in onPause() it is being set but as it is followed by onDestroy() the OS may forcly might cancelling the your task as this activity is finished.

Also if you set something in onPause() then if you application looses focus or is minimized the also onPause() is called might be your stabilty of app may lose. So suggested not use onPause(), better to go with either onKeyUp() or onBackPressed().

share|improve this answer
Actually that should not be a problem because according to the Dev Docs, the flow is set developer.android.com/guide/topics/fundamentals/… –  Shubhayu Apr 5 '12 at 7:42
add comment

Your Answer


