0

Is there a better way to handle (things that I want to handle when app is killed - by swipe up) in android apart from a not so better solution - using Services (As they always run on the main thread).

Just got this, https://stackoverflow.com/questions/26842675/continue-service-even-if-application-is-cleared-from-recent-app Still wanted to know is there any other alternative.

Basically what I want to achieve is, I need to do certain action(saving items to DB etc), only when the user initiates and kills the app from the recent list(swipe up and kill the app/clear all from Recent list) - how can i achieve this- best way to achive this

PrashanthR
  • 63
  • 6
  • This is untrue. Services do not "always run on the main thread". The whole point of a `Service` is that you can have background threads doing substantial work that is unrelated and not dependent on UI. – David Wasser Oct 21 '20 at 08:00
  • The question you linked to was created 6 years ago. Substantial changes have been made in Android to restrict background processing in the last 6 years. What exactly is it that you want to do? Your question is not clear. Please explain exactly the problem you have. – David Wasser Oct 21 '20 at 08:02
  • Thanks for the response, I need to do certain action(saving items to DB etc), only when the user initiates and kills the app from the recent list - how can i achieve this. I tried -> 1. ProcessLifecycleOwner - this is good for detecting when the app goes background and comes back to foreground, but I am not able to differentiate when the app is being killed by user as I mentioned above. 2. Tried to achive this by 'Service' - binding it to the activity , now this time only when the app is killed, the onTaskRemoved callback in the 'Service' class is called.But I dont think this the best way – PrashanthR Oct 22 '20 at 04:17
  • This is not possible. You cannot know when the user kills the app. Also, Android can kill the app at any time. You should always save items to the DB in `onPause()` because this is the ONLY lifecycle method that is guaranteed to be called. – David Wasser Oct 22 '20 at 09:22
  • Thanks. I was looking to see if there is a way looks like it s not as you stated – PrashanthR Oct 22 '20 at 12:30
  • hi https://meta.stackoverflow.com/users/1676363/ianhanniballake is there a way you could suggest. Thanks in advance – PrashanthR Oct 23 '20 at 11:35

0 Answers0