How to handle running service when app is killed by swiping in android?
AndroidAndroid Problem Overview
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.
Android Solutions
Solution 1 - Android
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.
Solution 2 - Android
We need to create a service that would clear the application from recent service
public class ClearService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ClearService", "Service Started");
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("ClearService", "Service Destroyed");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("ClearService", "END");
//Code here
stopSelf();
}
}
- register this service in
manifest.xml
<service android:name="com.package.ClearService" android:stopWithTask="false" />
- Then start this service on your splash activity
startService(new Intent(getBaseContext(), ClearService.class));
And now whenever you will clear your app from android recent Then this method onTaskRemoved()
will execute.
Solution 3 - Android
I resolved similar issue. If you want after swiping from recent task and on next launch it to behave properly then follow below steps:-
- Save process ID in shared preference:
SharedPreferencesUtils.getInstance().putInt(SharedPreferencesUtils.APP_PROCESS_ID, android.os.Process.myPid());
-
When application is launched from launcher after clear from recent task then do:
int previousProcessID = mSharedPreferencesUtils.getInt(SharedPreferencesUtils.APP_PROCESS_ID);
int currentProcessID = android.os.Process.myPid();
if ((previousProcessID == currentProcessID)) { // This ensures application not killed yet either by clearing recent or anyway } else { // This ensures application killed either by clearing recent or by anyother means }
Solution 4 - Android
When you press home - onPause
and onStop
of your Activity is being called, so at this time you have to do all savings and cleanup, because Android platform doesn't further guarantee that onDestroy
or any other lifecycle method would be invoked, so the process could be killed without any notification.
Solution 5 - Android
ViewModel.onCleared() can be useful, if the goal is to release some resource (perhaps a system running somewhere else on the network) when the user executes a surprise exit by swiping, rather than by pressing the "stop" or button. [This is how I originally arrived at this question].
Application doesn't get a notification, and Activity.onDestroy() gets called for configuration changes such as changes in orientation, so the answer isn't there. But ViewModel.onCleared gets called when the Application is swiped away (as well as when the user backs out of the activity). If the resource you want to use is associated with more than one activity in the stack, you can add reference counts or some other mechanism to decide if ViewModel.onClear should release the resource.
This is yet another of many good reasons to use ViewModel pattern
Solution 6 - Android
I don't really know why the above approaches are not working on my case even I set android:stopWithTask="false"
that onTaskRemoved()
not called.
Another good approach would be using AndroidViewModel
. This one even works on the case when user exits the applcation on pressing back button.
Just bound ViewModel
class to your MainActivity
then do your task onCleared()
callback.
Example:
public class MainViewModel extends AndroidViewModel {
public MainViewModel(@NonNull Application application) {
super(application);
}
@Override
protected void onCleared() {
// Do your task here
Log.e("MainViewModel", "OnCleared mainViewModel");
super.onCleared();
}
}
then bound it to your MainActivity:
MainViewModel viewModel = new ViewModelProvider(this).get(MainViewModel.class);
~ Voila!
Solution 7 - Android
You need to save your data when on onPause()
is called.
Look at this life cycle diagram:
Android Developer
You can see that an app can be killed after onPause()
or onStop()
.
Handle your data there and recover it in onRestart()
\ onCreate()
.
good luck!
Solution 8 - Android
You can't handle swipe, because system just removes your process from memory without calling any callback.
I have checked, that before user calls "recent apps" screen, onPause() will be always called. So you need to save all data in onPause method without checking isFinishing().
To check back button, use onBackPressed method.
Solution 9 - Android
As Bob Cram mentioned in his answer, View Model's onCleared() method is the answer. It works in both cases :
- When the user removes the app by swiping the app from background.
- When the user clear all the app using the clear list button.
Service's onTaskRemoved() will work when the user swipes the app from the background, but will not work when the apps are cleared using the kill all button.
But the viewModel's onCleared() method works in both cases. You can use if to stop any ongoing process or clearing any task in the remote server.
override fun onCleared() {
super.onCleared()
Log.d(TAG , "App Killed")
}
Solution 10 - Android
This worked for me on android 6,7,8,9.
Make one service like this:
public class OnClearFromRecentService extends Service {
@Override public IBinder onBind(Intent intent) {
return null; }
@Override public int onStartCommand(Intent intent, int flags, int
startId) {
Log.d("ClearFromRecentService", "Service Started");
return START_NOT_STICKY; }
@Override public void onDestroy() {
super.onDestroy();
Log.d("ClearFromRecentService", "Service Destroyed"); }
@Override public void onTaskRemoved(Intent rootIntent) {
Log.e("ClearFromRecentService", "END");
//Code here
stopSelf(); } }
2) Register this service in manifest.xml
:
<service android:name="com.example.OnClearFromRecentService"
android:stopWithTask="false" />
3) Then start this service on your splash activity
startService(new Intent(getBaseContext(),
OnClearFromRecentService.class));