Home » Android » How to handle code when app is killed by swiping in android?

How to handle code when app is killed by swiping in android?

Posted by: admin March 11, 2020 Leave a comment

Questions:

If my app is running and i press home button, the app goes in background. Now if a 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.

How to&Answers:

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:

  1. Inside your Manifest file, keep flag stopWithTask as false for Service. Like:

    <service
        android:name="com.myapp.MyService"
        android:stopWithTask="false" />
    
  2. Now in your MyService service, override method onTaskRemoved. (This will be fired only if stopWithTask is set to false).

    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.

Answer:

Found one way to do this

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));

And now whenever you will clear your app from android recent Then this method onTaskRemoved() will execute.

Answer:

I resolved similar issue. If you want after swiping from recent task and on next launch it to behave properly then follow below steps:-

1) Save process ID in shared preference:

SharedPreferencesUtils.getInstance().putInt(SharedPreferencesUtils.APP_PROCESS_ID, android.os.Process.myPid());

2) 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
}

Answer:

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.

Answer:

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!

Answer:

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.

Answer:

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));

Answer:

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.

— Bob

Answer:

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!