Issue
After reading official documentation about Activity Lifecycle I have an issue - How can I understand in onDestroy(), that my Activity was killed by system or finished by user? I think, at official page it's not fully clear:
If an activity is paused or stopped, the system can drop it from memory either by asking it to finish (calling its finish() method), or simply killing its process. When the activity is opened again (after being finished or killed), it must be created all over.
But little bit lower wrote about onDestroy() next:
Called before the activity is destroyed. This is the final call that the activity will receive. It could be called either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.
So, I suggest, that finish() can be called by system during killing my activity( So how can I distinguish - my activity was killed by system or finished by user in onDestroy()?
Solution
onDestroy() is not guaranteed to be called (e.g. your app is in the background and the OS actually kills it to attain resources), so there is not really any reason for you to check isFinishing() here.
Deal with what you need to in onPause(), checking isFinishing() there if you want to know if it is going on to finish.
If you want to do more in the specific scenario where the user has triggered an end to the app then add it to onUserLeavesHint(), which will be called just before onPause() - if this stuff needs to happen after some of the stuff you are doing in onPause() then you can have a boolean flag in the Activity to be set false from the get-go, then to true in onUserLeavesHint() and queried in your onPause().
Answered By - Jonathan Allan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.