如果每次按下home按钮然后返回活动时重新创建它,则与其关联的线程也将被重新创建。这意味着游戏循环将从头开始,因此当用户返回活动时,就像他们刚刚打开活动一样。
public class GameView extends SurfaceView implements SurfaceHolder.Callback{
protected GameThread gameThread;
public GameThread(Context context){
gameThread = new GameThread(this);
//the rest of the constructor
}
//gameview's stuff
}
//and in the thread...
public class GameThread extends Thread{
protected GameView gameView;
public GameThread(GameView gameView){this.gameView=gameView}
//gameview's stuff
}
在活动中创建:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GameView(this));