Keeping threads and connection state in Android app using onSaveInstanceState?

大兔子大兔子 提交于 2019-12-05 07:59:11

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!