@Override
public Object onRetainNonConfigurationInstance(){
final TicTacToeGame game = collectData();
return game;
}
private TicTacToeGame collectData(){
return mGame;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBoardButtons = new Button[TicTacToeGame.getBOARD_SIZE()];
mBoardButtons[0] = (Button) findViewById(R.id.one);
mBoardButtons[1] = (Button) findViewById(R.id.two);
mBoardButtons[2] = (Button) findViewById(R.id.three);
mBoardButtons[3] = (Button) findViewById(R.id.four);
mBoardButtons[4] = (Button) findViewById(R.id.five);
mBoardButtons[5] = (Button) findViewById(R.id.six);
mBoardButtons[6] = (Button) findViewById(R.id.seven);
mBoardButtons[7] = (Button) findViewById(R.id.eight);
mBoardButtons[8] = (Button) findViewById(R.id.nine);
mInfoTextView = (TextView) findViewById(R.id.information);
mHumanScoreTextView = (TextView) findViewById(R.id.player_score);
mComputerScoreTextView = (TextView) findViewById(R.id.computer_score);
mTieScoreTextView = (TextView) findViewById(R.id.tie_score);
mGame = new TicTacToeGame();
startNewGame();
final TicTacToeGame game = (TicTacToeGame)getLastNonConfigurationInstance();
if (game == null)
{
game = collectData();
}
}
当设备方向从纵向切换到横向时,Layout
负载正常,但没有保存任何内容,并且分数被重置,这意味着所有数据都丢失了。该game = collectData();
部分还要求我删除最终修饰符
final TicTacToeGame game = (TicTacToeGame)getLastNonConfigurationInstance();
有任何想法吗??