Issue
If my app is running and i press home button, the app goes in background. Now if I long press the home button and kill the app by swiping it from the recent app list, none of the events like onPause()
, onStop()
or onDestroy()
gets called rather the process is terminated. So if i want my services to stop, kill notifications and unregister listeners, how can i do that? I read quite a few articles and blogs but didn't get any useful information and I haven't found any documentation about it.
Any help would be appreciated.
Thanks in advance.
Solution
I just resolved a similar kind of issue.
Here is what you can do if its just about stopping service when application is killed by swiping from Recent app list.
Inside your Manifest file, keep flag stopWithTask
as true
for Service. Like:
<service
android:name="com.myapp.MyService"
android:stopWithTask="true" />
But as you say you want to unregister listeners and stop notification etc, I would suggest this approach:
Inside your Manifest file, keep flag
stopWithTask
asfalse
for Service. Like:<service android:name="com.myapp.MyService" android:stopWithTask="false" />
Now in your
MyService
service, override methodonTaskRemoved
. (This will be fired only ifstopWithTask
is set tofalse
).public void onTaskRemoved(Intent rootIntent) { //unregister listeners //do any other cleanup if required //stop service stopSelf(); }
Refer my question for more details, which contains other part of code, too.
Hope this helps.
Answered By - MysticMagicϡ