2

I have an app that handles an XMPP connection. Now this connection is closed and resumed correctly when the main (and only) activity goes onStop(). Problem is, in the particular case when the user: -is using the app -open the recent apps list (eg square button) -kills the app

I receive no callbacks since the process gets killed (at least as far as I understand).

This is causing some weird behaviours since if the user opens the app again in a short time, there're two active connections for the user.

Aside from these connection duplicates, what is the right way to handle this specific case? Most of the answers here are quite outdated and most suggest using services to keep track of activities removed/added, but this wouldn't work on this case anyways since the service would not receive the event (the process got killed), correct? I would also run into the limitations for backround services introduced in Android 8, making the whole thing unreliable

Is there something I missed?

jack_the_beast
  • 1,838
  • 4
  • 34
  • 67
  • Did you use any of the Lifecycle-aware libraries? If you happened to use the `ViewModel` you can close or dispose your connection in `onClear()`. – Sovathna Hong Apr 30 '22 at 06:14
  • I tried AndroidViewModel, but onClear() is not called in mentioned case – jack_the_beast May 02 '22 at 07:33
  • I also tried ViewModel to no avail – jack_the_beast May 02 '22 at 07:48
  • Is your app a Single Activity app? If yes why don't you use onDestroy() call? – Alex Rmcf May 16 '22 at 09:55
  • When your process gets killed there is nothing you can do about it. Even if there were some mechanism for notifying your application about its being killed, you really should not expect it to work correctly across all devices. Moreover, your application process could just die because of some unhandled exception. In such situations, you might not have any possibility to close your connection properly and you will face the same issue anyway. – 0awawa0 May 16 '22 at 10:41
  • I am not familiar with the XMPP protocol itself, so I don't know what capabilities you have there. But the problem itself seems to me like poor session handling, either on the backend side or on both sides. So there might be (should be actually) a way of either detecting duplicate sessions on the server side and forcing sessions to be renewed, or saving session information in your application so it can be reused if the application dies. – 0awawa0 May 16 '22 at 10:50
  • @AlexRmcf onDestroy doesn't get called in such case – jack_the_beast May 16 '22 at 15:26
  • @0awawa0 agreed, the problem is most certainly backend-sided, although we an outdated version of xmpp and related libraries on both ends, which we cannot upgrade atm. I suspected the situation was the current one but I wanted to be sure that there's nothing we can do to solve this problem, if you post an answer I'll gladly give you the bounty. – jack_the_beast May 16 '22 at 15:30
  • When you would clear app from recent app list then nothing would call therefore you could deal this situation in a way that when you would open app you could first check whether last connection is opened or not, if it's opened route to session screen otherwise nothing would happen. – Ali Azaz Alam May 23 '22 at 07:03

1 Answers1

0

I'm not sure if this would solve the problem but wouldn't you get the callback when the user opens the recent apps list with onPause().

Draguve
  • 21
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 23 '22 at 07:02
  • while onPause() would be called, there's no way of knowing if it was called because the tasks list was opened. you can't open/close the connection at every onPause()/onResume() since this cycle also happens for dialogs (for example). we are currently using onStop(), but as described in the other comments, this is not enough – jack_the_beast May 23 '22 at 08:34