Issue
Can onActivityResult()
theoretically be called before onCreate
? I know it can be called before onResume()
, but let's consider this extreme case:
- activity A is running and starts activity B for a result
- activity B opens
- the device is running low on memory, so it destroys activity A
- activity B finishes with a result
What happens now? Does activity A get re-created before receiving the result, or does it receive the result before onCreate()
? Is this guaranteed to be in the same order each time?
Solution
You will commonly see this if you are opening an app to scan a barcode or take a photo or video. The Activity
you launch to do that requires a lot of resources so Android kills the OS process hosting your app.
When the Activity
you launched wants to return the result to your app, the following occurs:
- Android creates a new OS process for your app (because it killed yours and needs a new one)
- Android instantiates the
Application
instance (or your app's custom one) and callsonCreate()
on that - Android instantiates a new instance of the
Activity
that will get the result (the one that calledstartActivityForResult()
) - Android calls
onCreate()
on the newActivity
. TheBundle
passed toonCreate()
in this case is theBundle
most recently used to the save theActivity
instance state (fromonSaveInstanceState()
). - Android calls
onStart()
on the newActivity
- Android calls
onActivityResult()
on the newActivity
- Android calls
onResume()
on the newActivity
Answered By - David Wasser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.