Search code examples
androidandroid-serviceandroid-architecture-componentsandroid-jetpackandroid-workmanager

How to cancel any/all work manager request if the app is removed directly from app tray?


I'm trying to use Android WorkManager in an application in order to schedule some background task. The requirement is to cancel the API request when the app is removed directly from app-tray.

I'm able to detect if app is getting removed from app-tray, by using Service class -> onTaskRemoved method. And here I'm making the WorkManager API cancel request.

public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);
    Log.d("onTaskRemoved","onTaskRemoved called");
    WorkManager.getInstance().cancelAllWork();
}

This is working fine as expected. Please suggest if there is any other better alternative to achieve it.


Solution


  • For some android devices (for some vendors) they implemented like if any apps is removed from app tray means they will force stop the app.So the services,work manager are also stopped.


    For some other android devices (for some other vendors) they implemented like if any apps is removed from app tray means they will stop/kill only the current activity of the app & not force stop the app.So the services,work manager are not stopped.
    First you need to check your target devices is comes in which category.