Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping threads and connection state in Android app using onSaveInstanceState?

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!

like image 658
Eitan Avatar asked Nov 06 '10 09:11

Eitan


People also ask

What is onSaveInstanceState in Android?

The onSaveInstanceState() method allows you to add key/value pairs to the outState of the app. Then the onRestoreInstanceState() method will allow you to retrieve the value and set it back to the variable from which it was originally collected.

What should be done to preserve the state of an activity?

When the activity goes into the background, the system calls onSaveInstanceState() . You should save the search query in the onSaveInstanceState() bundle. This small amount of data is easy to save. It's also all the information you need to get the activity back into its current state.

When onSaveInstanceState is called?

Note that onSaveInstanceState() is called when your activity goes into the background and NOT when the app process is about to be killed.

How the activity comes to foreground?

Activity or dialog appears in foreground When the covered activity returns to the foreground and regains focus, it calls onResume() . If a new activity or dialog appears in the foreground, taking focus and completely covering the activity in progress, the covered activity loses focus and enters the Stopped state.


1 Answers

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.

like image 183
CommonsWare Avatar answered Sep 30 '22 09:09

CommonsWare