1

I am new to Android and making my first real app and it uses the socket.io Java Client for communicating with my Node.JS server instance. I have recently discovered while testing that the OS can kill background apps whenever it feels necessary calling onSaveInstanceState() on the active Activity then when you switch back to the app it recreates the previous state using the Bundle passed as an argument in the onCreate() method.

So now that I know that, I need to implement a solution that would keep the app working as it should so I expect I will have to test a few solutions before I get the right one. And since it would be so time-consuming to just lock my screen and watch the server logs waiting for the

User disconnected

(which can take a lot of time for each try), I wondered if anyone here ever was in the same situation and figured a way to make the OS kill the app as soon as I lock the screen or hit the Volume Down button or something like that.

Mouradif
  • 2,666
  • 1
  • 20
  • 37
  • Have you tried "Developer Options / Do not Keep Activities" at device settings? With this option activities are being destroyed immediately as soon as the user leaves them. – Devrim Sep 22 '16 at 10:24
  • I've just tried this : it seems like `onSaveInstanceState()` is called but the `socket.io` connexion keeps active whereas when the app gets killed after long screen lock, the `socket.io` connexion stops – Mouradif Sep 22 '16 at 10:38
  • Also just tried removing this option, `onSaveInstanceState()` is called anyway as soon as i hit the Home button – Mouradif Sep 22 '16 at 10:45
  • With this option your activity's onDestroy method will be invoked when you leave your activity. Maybe you can close connection under onDestroy method programmatically? – Devrim Sep 22 '16 at 10:55
  • If I close the connection under `onDestroy()` of every Activity that uses `socket.io`, I will lose all the benefits of having a persistent connection. – Mouradif Sep 22 '16 at 12:19
  • I'm a little bit confused. Are you looking for an answer to kill your app any time? If so check out this link: http://stackoverflow.com/questions/3117095/stopping-an-android-app-from-console – Devrim Sep 22 '16 at 14:15
  • What I am currently looking for to be precise is a way to replicate the natural behaviour of Android (ie. killing background apps) the way it usually do. I think the closest I got is with your first comment but for some reason it seems like the app is not really killed even though the activity is. I know that because the `socket.io` connexion isn't terminated with this method. – Mouradif Sep 22 '16 at 14:58

0 Answers0