Return to SurfaceView after another

I am working on an Android RPG using the demo version of the LunarLander API. I already made a game using this demo (Porcupine Assassin, check it out!), So I am well versed in the Canvas class and things like that.

My problem is that in RPG you need a way to access inventory, statistics, etc. So I set the BACK button to run the Inventory.java class. I am having problems when I end () the Inventory activity and try to return to the game (SurfaceView).

This is the SurfaceCreated () callback:

public void surfaceCreated(SurfaceHolder holder) {
    thread.setRunning(true);
    thread.start();
}

I was getting an FC called by "IllegalThreadStateException: thread is already running", so I set try / catch in the SurfaceCreated () callback. There is no FC with try / catch, but I am returning to the black screen.

I tried to take try / catch and add a check at the beginning: if (! Thread.isAlive ()). Thus, if the thread is already running, it will do nothing. Oddly enough, I have the same FC “thread” that is already running, so thread.isAlive () should have 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 am very interested in the game side of this, I have most of the basics for my RPG. But all these Android / Java things that go over my head. Can someone point me in the right direction?

+3
source share
1 answer

, , .IsAlive() false. , google LunarLander - .

, , :

    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();
        }

+4

Source: https://habr.com/ru/post/1768682/


All Articles