1

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) {
        }
    }       
}
4

1 に答える 1

0

私の経験では、戻るボタンはアクティビティを終了するため、アクティビティを再開すると onCreate メソッドが再度呼び出されます。

代わりに、ホーム ボタンはアクティビティを終了しません。ホーム ボタンを押すと、アクティビティの onPause メソッドが呼び出され、再起動すると呼び出されるメソッドは onResume です。

SurfaceView の作成を担当する onCreate にいくつかのコードがあると思います。このコードを onResume メソッドに追加してみてください (オーバーライドします)...

于 2010-04-06T15:10:18.040 に答える