8

I have a question about the Activity's state while in onActivityResult. Specifically, is it 'guaranteed' that either onRestoreInstanceState or onCreate have been called prior to onActivityResult for an Activity? In other words, is it safe to assume that the state data (member variables and such) of the Activity are 'usable' while in onActivityResult (assuming you have properly handled onRestoreInstanceState and or onCreate)?

steve
  • 303
  • 1
  • 4
  • 11

2 Answers2

19

In my app I get the following workflow:

onCreate
onStart
onRestoreInstanceState
onActivityResult
onResume

So, yes onActivityResult is called AFTER the onRestoreInstanceState, so you can count on the state has been fully restored (unless you do smth in onResume).

Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91
  • Thanks, it makes sense that it would work that way but I was just double checking as I can't find it documented anywhere. – steve Feb 21 '11 at 14:06
  • this answer is wrong: http://stackoverflow.com/questions/6468319/onactivityresult-onresume – kmalmur Aug 01 '14 at 07:53
  • @mack369: could you please tell what exactly is wrong? – Vit Khudenko Aug 01 '14 at 20:24
  • 5
    onActivityResult is called before onStart in case you launch another activity for result and go back to the initial activity – kmalmur Aug 02 '14 at 21:36
  • Judging by my logs, onStart is most definitely called before onActivityResult, which is followed by onResume. The question you linked to doesn't seem to mention onStart at all. May I ask where you found this info? – Riot Goes Woof Jul 02 '15 at 22:03
  • 1
    OS does not kill Activity while in background; it would rather kill whole process – Marian Paździoch Feb 02 '20 at 14:19
  • 1
    @MarianPaździoch, thanks, just edited and removed the confusing sentence. It was written in 2011, when I initially believed (due to (a) misleading documentation from Google and (b) Developer Options allowing to force such behavior) that OS could kill separate activities. – Vit Khudenko Feb 02 '20 at 21:51
-1

An activity which launches another activity with startActivityForResult is fully active, just it has lost the focus - i.e it does not recieve touch input. If you would launch a transparent activity with this method you could see that the first activity is still doing its animations etc in the background.

Lumis
  • 21,517
  • 8
  • 63
  • 67
  • 1
    After calling startActivityForResult, onSaveInstanceState is called. Android calls this so that if it needs to remove the Activity from memory it can restore it later (either providing the state information in a bundle via onRestoreInstanceState or onCreate). So, although an Activity *may* still be running after you call startActivityForResult I don't believe you can guarantee it will not be killed by Android once the new Activity has started. So, my question is if Android needs to call onRestoreActivityState or onCreate, will that always be before or after onActivityResult is called? – steve Feb 20 '11 at 19:46
  • onActivityResult is more like a listener which is waiting for input, so my good guess is that both activities can be killed without ever having onActivityResult being executed. – Lumis Feb 20 '11 at 19:55