0
 @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(); 

有任何想法吗??

4

3 回答 3

1

onRetainNonConfigurationInstance已弃用。请不要使用它。

如果您正在使用片段,那么您应该使用setRetainInstance

如果您使用的是活动,我更喜欢以下方法,

您应该onSaveInstanceState用于此目的。

@Override
public void onSaveInstanceState(Bundle outState) {
   super.onSaveInstanceState(outState);
   outState.putString("message", "This is my message to be saved when activity is restarted.");
}

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  if( savedInstanceState != null ) {
     savedInstanceState .getString("message") //restore data
  }
}
于 2013-03-14T11:22:44.070 回答
0

当方向改变时,系统重新启动当前活动。您需要在适当的 android 方法中保存所需的任何变量。请参阅: Android 数据存储

于 2013-03-14T11:23:32.047 回答
0

当设备配置更改时,活动将被销毁并重新创建。您必须保存您的数据。http://developer.android.com/guide/topics/resources/runtime-changes.html。如果您正在恢复活动娱乐的大型数据集,请查看标题 在配置更改期间保留对象下的链接。

@Override
public Object onRetainNonConfigurationInstance() {
final MyDataObject gamedata = collectMyLoadedData();
return data;// returm game data object
} 

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
if (data == null) {
   //supple your game data
   //load game data with the data object.
  }

}

对于小数据集,您可以使用以下内容。(仅保留游戏得分数据)

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putString("scores",scorevalue);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
 super.onRestoreInstanceState(savedInstanceState);
 // Restore UI state from the savedInstanceState.
 // This bundle has also been passed to onCreate.
 gameScore = savedInstanceState.getString("scores");
}
于 2013-03-14T11:22:19.560 回答