0

I have a ViewSwitcher in a FragmentActivity with in 2 layout files, one for each orientation. The ViewSwitcher is controlled by a radio group.

When I rotate the screen, everything behaves as I'd expect, the correct layout file is used to render the screen.

However, The radio group's onCheckedChanged event is fired when the screen rotates.
The listener for that looks like this (created in onCreate):

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        //snip
        radios.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int index) {

            viewSwitcher.showNext();
        }
        });
 }

How can I call viewSwitcher.showNext() in the listener without having it fire when the screen is rotated?

GAMA
  • 5,958
  • 14
  • 79
  • 126
Ben
  • 16,124
  • 22
  • 77
  • 122

1 Answers1

0

well, I sorta found a hacky solution, but I'm hoping there's a better one out there.

    private boolean wasRotatedHack;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.savedInstanceState = savedInstanceState;
    if (savedInstanceState == null) {
        wasRotatedHack = false;
    } else {
        wasRotatedHack = true;
    }

    tabRadios.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int index) {

            if (!wasRotatedHack) {
                viewSwitcher.showNext();
            } else {
                wasRotatedHack = false;
            }
        }
    });

    startInspectionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(getApplicationContext(), InspectionActivity.class));
        }
    });
  }
Ben
  • 16,124
  • 22
  • 77
  • 122
  • just tested this a bit more, it gets messed up when you turn the screen off and come back on. there must be a cleaner way... – Ben Jun 21 '12 at 07:23