Android game goes blank after pressing home and restarting

Posted by sirconnorstack on Stack Overflow See other posts from Stack Overflow or by sirconnorstack
Published on 2010-04-06T13:28:54Z Indexed on 2010/04/06 13:33 UTC
Read the original article Hit count: 191

I am making an Android game with an Activity called Game, a SurfaceView called GameView and a Thread called GameThread. The Game's onCreate(), I make a new GameView, which makes a new GameThread, where all the game logic and canvas drawing is carried out.

However, I'm having some lifecycle difficulties. When I press back and restart it, it works fine, but when I press home and restart it, all I see is a blank screen. Here are the GameView's onSurfaceCreated() and onSurfaceDestroyed():

@Override
public void surfaceCreated(SurfaceHolder holder) {
    mThread.mRunning = true;
    mThread.mWidth = getWidth();
    if(mThread.mWidth > 550) mThread.mWidth = 550;
    mThread.mHeight = getHeight();
    try {
        mThread.start();
    } catch(IllegalThreadStateException e) {
    }

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // Wait for the secondary thread to end before finishing
    boolean retry = true;
    mThread.mRunning = false;
    while (retry) {
        try {
            mThread.join();
            retry = false;

        } catch (InterruptedException e) {
        }
    }       
}

© Stack Overflow or respective owner

Related posts about android

Related posts about game-development