简体   繁体   中英

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();
    }

}

When device orientation is switched from portrait to landscape, the Layout loads fine but nothing is saved and the scores are reset meaning all data is lost. The game = collectData(); part as well is asking me to remove the final modifier on

final TicTacToeGame game = (TicTacToeGame)getLastNonConfigurationInstance(); 

Any ideas??

onRetainNonConfigurationInstance is deprecated.Please dont use it.

If you are working with fragment then you should use setRetainInstance

I would prefer following method in case if you are using Activity,

You should use onSaveInstanceState for this purpose.

@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
  }
}

When device configuration changes the activity is destroyed and recreated. You must save your data. http://developer.android.com/guide/topics/resources/runtime-changes.html . If your recovering large data set on activity recreation have a look at the link under the heading Retaining an Object During a Configuration Change .

@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.
  }

}

For small data set you can use the following.(retain just game score data)

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

When the orientation is changed, the system restarts the current activity. You need to save any variables that you need in the appropriate android method. See: Android data storage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM