Currently i'm working on an update of my app money is time were I've ran into the following problem. I would like to support landscape and portrait orientation so I've created two different layouts.
My first problem was that after rotation the application restarted and the number set where flushed.
This was easy to fix after finding the this question here on stackoverflow.
I've implemented the following solution for that problem:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setGui();
}
I've tested it with the following code where the layout of the wheels are adjusted depending on the layout. Which again all works like it should. When i change the orientation, the numbers don't change.
public void setGui() {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
initWheel(R.id.money1, 0, 9, "%1d", moneyWidthPortrait, textSizePortrait);
...
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
initWheel(R.id.money1, 0, 9, "%1d", moneyWidthLandscape, textSizeLandscape);
...
}
}
But I also want to change the background and the layout (padding and margins) so I've added the following two lines:
public void setGui() {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(portrait);
...
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(landscape);
...
}
}
But after calling setContentView all the data seems to be lost again and the wheels are reset to zero after changing the phones orientation.
What would be the best to avoid that the wheels are reset to '0' during orientation and layout change?
EDIT: Edits after answer
I've removed:
android:configChanges="keyboardHidden|orientation"
and added:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("WheelMoney1", getWheelValue(R.id.money1));
...
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
getWheel(R.id.money1).setCurrentItem(savedInstanceState.getInt("WheelMoney1"), false);
...
}
And implemented layout-land
and layout-port
for handling the two different layouts.