The result is not set in onPause (), using setResult () when you press the back button

I have 2 actions AAA and BBB. I call BBB from AAA using startActivityForResult (Intent, int) . After I finished with BBB, I click the back button to return to AAA. In BBB, I override onPause () and set the result using setResult (RESULT_OK) .

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

After spending some time in google / stackoverflow, I realized that if I override onBackPressed () and set the result in it, then it works absolutely fine.

What I don't understand is why the result is not set in onPause (), when onPause () is actually called after onBackPressed (). I went through activity streams in Dev docs, and I understand quite clearly what was mentioned there.

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

+5
source share
3 answers

You should take a look at the link onActivityResult. 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 setResultin finish(). Besause onPause()can be called when a new action starts with BBB .

+4
source

, , onPaused , . . Log.d, onPause onActivityResult.

0

, .

BBB acctivty, Pause() , - onPause(), onStop() : OnDestroy(), . , - onPause(), , onDestroy() , , .

Also, if you set something in onPause (), then if the application loses focus or is minimized, the onPause () call is also called, which can lose your application stability. Therefore, it is suggested not to use onPause (), it is better to go with either onKeyUp () or onBackPressed ().

0
source

All Articles