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 に答える