Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiate between Android killing the app and user swiping it off on the recent apps list

I am working on a project, where while being on a specific Activity we show a local sticky notification. That should also be the case when the app is minimized. What I have to accomplish is to remove the local notification whenever the app is killed (by Android, because of memory lack or by the user, with a swipe from the recent apps list).

Usually onDestroy would be called whenever Android takes the Activity to open some space. That is fine in one of the cases, however swiping an app from the recent app lists doesn't call the onDestroy and the sticky notification stays.

What I did is, I implemented an empty Service which would force the onDestroy when the app is killed (both swipe and system kill) so I can get my notification removed.

However, what I would like to do is to differentiate between the swipes and system kill.

Is this even possible?

like image 524
Nikola Avatar asked Jun 19 '15 11:06

Nikola


2 Answers

In general, if Android wants to kill your application because it has been in the background for too long (or because it wants to reclaim resources), Android will just simply kill the OS process hosting your app. It will not call finish() or onDestroy() on any Activity or Service components. The behaviour of "swipe from recent tasks list" has changed over time and is different in different Android versions. Someone should write a book about that :-(

like image 57
David Wasser Avatar answered Oct 12 '22 01:10

David Wasser


You can check for when the user swipe-closes the app by adding a service to your app, and implementing the onTaskRemoved method: https://stackoverflow.com/a/26882533/2441655

like image 45
Venryx Avatar answered Oct 12 '22 03:10

Venryx