1

I need to perform some APIs calls if the app is closed.

onDestroy() and onStop() wouldn't be fired if the app is closed from recent apps list or killed by the android system. But there may be a workaround for that?

I've tried this & this

when I tried (on Xiaomi note 7 Android 9) it fails and not call it too and from comments, all agreed that onTaskRemoved() no more called in Android O.

so can anyone help me to find a way to achieve that workaround especially on Xiaomi, oppo, etc?

Community
  • 1
  • 1
Moustafa EL-Saghier
  • 1,721
  • 1
  • 13
  • 43

1 Answers1

1

I assume the problem is not with onTaskRemoved not being called when you are removing the app from the recent list. I would say, maybe the service that you started was killed before the app went into the recent apps list.

I would like to share an answer here at SO, which tries to describe the problem of foreground services being killed.

Hence, if you run the service as a foreground service as suggested here, you need to do something on a periodic basis to keep the service alive. I assume you should be able to get the onTaskRemoved called if the service was not killed before.

Suggestions based on your use-case:

As mentioned in the comment of this answer, you want to remove the items from the cart when the user has left the app, to make it available to order for other users.

This is more like a design problem to me which can be done in many ways. I can drop some suggestions, however, my suggestions might not fit in your case.

  1. Solving it from server-side:
    1. When a user is adding an item in the cart, store the timestamp value when it was added to the cart, in your server-side database.
    2. Take a default threshold for the expiry of the cart items. When a new user from another device is trying to access the same item, check the timestamp value and mark that item as available if the time is passed the threshold time.
  2. Solving it from server-side 2:
    1. IMHO, an item is always available until the item is checked out/paid. In your implementation, if I can get your API endpoints, I can make a simple scripting attack, so that I can hold the item in my cart without buying it. Hence there is a serious vulnerability in your current implementation which can be exploited easily. I would rather keep the item available in the cart until someone pays for it.
    2. If you keep the item available to order until someone pays for it, you can check the availability just before making the payment and tell the customer that the item is already sold.
  3. Solving this from the app side:
    1. I would say, I could not find a good way to ensure that the API for cart cancellation was called to remove the items from the cart. The solution that I proposed earlier, still might work, however, IMHO, this is overkill.
    2. If you take the onTaskRemoved implementation, you might consider having a timer there as well so that you can also have a threshold value for the items to be removed from the cart, after a certain period of time. Otherwise, you will end up keeping the item unavailable for other users forever (as I stated above).
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • before the move to the periodic task, the `onTaskRemoved ` never executed although the service is started already my phone is Xiaomi note 7. also, must I've periodic logic here? Do I need if the app already closed no need it run long time you got what I mean? – Moustafa EL-Saghier Feb 03 '20 at 19:34
  • I am not sure about your intention, however, I was trying to state that, you need to start the service as a foreground service so that user can see the service running as a notification or something even if the app is in the background. Also, the foreground service must execute some periodic tasks in order to keep itself alive. – Reaz Murshed Feb 03 '20 at 19:37
  • let me explain for u, my backend works as following .. hold on service that added to cart, if the user cancels the booking I clear it, but, what if he closed app? what I need is clear cart items in case the app was closed. so that release services for other users if u know better way to remove items from the cart please suggest. – Moustafa EL-Saghier Feb 03 '20 at 19:42
  • I have updated my answer. Please have a look. Thank you! – Reaz Murshed Feb 03 '20 at 19:57
  • Can we invoke restart of service once it's been killed from `onTaskRemoved`? – IgorGanapolsky Apr 28 '20 at 03:34
  • 1
    @IgorGanapolsky I am not sure about that actually. However, IMHO, I would not recommend doing that as well. We can use `JobScheduler` or android's foreground service to check in a periodic manner if the app was stopped or not. – Reaz Murshed Apr 28 '20 at 14:46