Issue
When my app is starting, I created a periodic work request using WorkManager to fetch the data from the server and its working fine. My problem is, I want to cancel the work request when my app is closing.
How to cancel the work request when my app is closing or how to detect the app is closing?
I would appreciate any suggestions.
Solution
As mentioned in the Android Documentation.
In Kotlin
WorkManager.cancelWorkById(workRequest.id)
In Java
WorkManager.cancelWorkById(workRequest.getId());
You can also cancel WorkRequests by tag using WorkManager.cancelAllWorkByTag(String). Note that this method cancels all work with this tag. Additionally, you can cancel all work with a unique name using WorkManager.cancelUniqueWork(String).
EDIT
To check help in app closing events, check the ProcessLifecycleOwner.
Kotlin example
https://github.com/jshvarts/AppLifecycleDemo
Answered By - Rahul Khurana
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.