Reputation: 49
I want to restart my app when it is removed from recent list. I have added the restarting code in onDestroy()
method. It's working fine in Android 7 and up. But on Android 6 and older versions, it's not working. onDestroy()
method is called only when back button is pressed.
Upvotes: 2
Views: 3052
Reputation: 2441
onDestroy()
not being called is quite an obvious behaviour. When a user swipes from the recent tasks, the app is killed immediately without the lifecycle methods being called. If you go through onDestroy() documentation you will find:
Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.
But there is a workaround.
Visit THIS StackOverflow answer if you want to handle the task killing event on API level below 26. This will work for sure.
Upvotes: 1