1

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.

Community
  • 1
  • 1
patrick
  • 1,282
  • 4
  • 21
  • 37

2 Answers2

3

It sounds like you need to implement onSaveInstanceState and onRestoreInstanceState

mjr
  • 1,963
  • 4
  • 20
  • 21
2

You shouldn't need to call setContentView when changing orientations. Android will do this for you if you save your layout with the same name under the correct layout directory. For example:

res/layout-land/main.xml
res/layout-port/main.xml

When your Activity switches orientation, Android will load the correct layout.

Steve Prentice
  • 23,230
  • 11
  • 54
  • 55
  • This didn't work in the situation described above. This does work when removing the android:configChanges="keyboardHidden|orientation", but then the setContentView is called every time in the onCreate which doesn't solve the problem. But thanks for your answer. I've removed this statement and implemented the onSaveInstanceState as suggested in the other answer and added the two folder as you described. – patrick Jun 14 '11 at 18:07