2013-03-14 51 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