2013-03-14 59 views
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(); 
    } 

}Android的方向變化和佈局

當設備方向從縱向切換到景觀,Layout加載罰款,但沒有被保存,分數被重置意義的所有數據都將丟失。 game = collectData();部分以及要求我刪除最終修改器上

final TicTacToeGame game = (TicTacToeGame)getLastNonConfigurationInstance(); 

任何想法??

回答

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"); 
} 
1

onRetainNonConfigurationInstance是deprecated.Please不使用它。

如果您正在使用片段的工作,那麼你應該使用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 
    } 
} 
0

當方向改變時,系統重新啓動當前活動。你需要在適當的android方法中保存你需要的變量。 參見: Android data storage