Question

I am developing a multi player game app for the android. One of the participant is acting as the host (the one who created the game instance) and each of the other participants is connecting to the host using bluetooth.

My question is as follows, this host has some threads running in order to communicate and holds all the open connections. I've read that my Activities can be destroyed temporary and restored later and I should use the onSaveInstanceState mechanism for that. However, I am talking about an app that is acting as a "server" for the game and it has open connections and threads that serves the other clients, what happens to these connections and threads if the OS is deciding to destroy my activity? Are they terminated? If so what would be the recommended pattern in order to implement this properly, How can I keep a connection on the onSaveInstanceState bundle? it is not serializable. The same goes for the threads, do I need to recreate and destroy them upon destroy and when the activity is restored?? if I hold all this state in some static class that represents the game state? If I do so, will then the OS not destroy my threads/connections?

I looked at the sample Bluetooth chat that comes with the SDK and I so there's no handling of the onSaveInstanceState so it is very unclear what should I do.

Thank you!

Was it helpful?

Solution

what happens to these connections and threads if the OS is deciding to destroy my activity?

If you created those threads in an activity, you better stop those threads, or you will leak them.

How can I keep a connection on the onSaveInstanceState bundle?

You can't.

The same goes for the threads, do I need to recreate and destroy them upon destroy and when the activity is restored?

If they were started by the activity, then, yes, please.

if I hold all this state in some static class that represents the game state? If I do so, will then the OS not destroy my threads/connections?

Please put your server logic (threads, connections, server-side game logic) in an Android service. Please use startForeground() to keep that service going, putting a Notification in the status bar along the way so the user has an easy way to get back to the activity that allows them to stop your service.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top