I have an Activity with a SurfaceView that is managed by a game engine (thread). I’m overriding the onCreate and the onSaveInstanceState for managing the save/restore state of the thread.
When the preferences screen is accessed from the menu of that Activity, that Activity goes through its lifecycle to onStop; when the preferences screen is closed, the Activity starts again at onResume, so onCreate does not happen. The thread however is in a state of TERMINATED and therefore cannot be resumed.
Yes, I can just create a new instance of the thread–but how can I restore the state? I tried overriding onRestoreInstanceState as well, but after a quick test and reading a little more thoroughly I realized that it’s not called in this situation because it’s called between onStart and onResume.
What’s the best way to handle this?
The problem seems to be with the dirty design of my app. Since the game engine itself is extending the Thread class and automatically being set to
TERMINATED, my only option with the current design is to add logic to thesurfaceCreatedmethod. In there, I can check the state of the thread:If it’s
TERMINATED, I can create aBundleand pass it to my engine’s saveState method. Once I have the state, I make the thread a new game engine thread and pass in the recently savedBundle. I think continue as normal (start the thread).This works, but the best way would be to move the actual Thread logic to a separate class. Since the game engine isn’t actually being cleared, I would just be creating a new
Threadinstance instead of recreating the entire game engine, so this is what I’ll do.