Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 702
Shraddha Wankar Avatar asked Nov 28 '25 02:11

Shraddha Wankar


1 Answers


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.

like image 64
SIVAKUMAR.J Avatar answered Nov 29 '25 17:11

SIVAKUMAR.J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!