[英]Android Orientation Change and layout
@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();
有任何想法吗??
不推荐使用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
}
}
设备配置更改时,活动将被销毁并重新创建。 您必须保存您的数据。 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");
}
更改方向后,系统将重新启动当前活动。 您需要在适当的android方法中保存所需的任何变量。 另请: Android数据存储
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.