Returning to SurfaceView after another Acitivity has taken focus
I'm working on an RPG开发者_如何转开发 for Android, using the LunarLander API demo. I've already made a game using this demo (Porcupine Assassin, check it out!) so I've got a good grasp of the Canvas class and things of that nature.
My problem is, in RPGs you need a way to access inventory, stats, etc. So I've set the BACK button to start the class Inventory.java. I'm running into problems when I finish() the Inventory activity and try to return to the game (the SurfaceView).
This is the SurfaceCreated() callback:
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}
I was getting an FC caused by "IllegalThreadStateException: thread already started" so I put a try/catch in the SurfaceCreated() callback. With the try/catch, no FC happens, but I return to a black screen.
I tried taking the try/catch out and adding a check in the beginning: if(!thread.isAlive()). That way, if the thread was already started, it wouldn't do anything. Oddly enough, I got the same FC "thread already started" so thread.isAlive() must've returned false.. ??
I also have the onWindowFocusChanged() method from the API:
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
if (!hasWindowFocus) thread.pause();
else thread.setState(STATE_RUNNING);
}
I'm pretty keen on the gaming side of this, I've got most of the groundwork for my RPG laid out. But it's all this Android/Java stuff that goes over my head. Can anyone point me in the right direction?
It is because you are trying to restart a terminated thread, thus the .IsAlive() returned false. There is a lot of documentation on this problem if you google LunarLander bug or illegal thread or something.
One possible workaround I found one time was this:
public void surfaceCreated(SurfaceHolder holder) {
if (thread.getState() == Thread.State.TERMINATED) {
thread = new CascadeThread(getHolder(), getContext(), getHandler());
thread.setRunning(true);
thread.start();
}
else {
thread.setRunning(true);
thread.start();
}
You can find the full documentation here
精彩评论