Gallery calls onItemSelected after orientation change

I have a Gallery which allows users to browse a gallery, select an image as their choice, and continue to browse the gallery without changing that selection. This works by using onItemSelected to see if the image they are selecting (ie, with a click) is the image already selected (by the gallery's motion). So:

  1. User moves forward and back through Gallery.
  2. User decides on their favoured image and centres the Gallery on it.
  3. User clicks the image, it gets "selected" and is given a green border.
  4. User can continue to move forward and back in the Gallery, without the currently centred image automatically being "selected".

This is fine. However, I am now trying to allow orientation changes while selecting an image, and what I have found is that onItemSelected is called on the Gallery after all the onConfigChanged code has executed.

So onConfigChange I set parameters (ie, what index the Gallery was on before orientation change), and then I call my initialiseUI method. This executes successfully and sets up everything it needs to, including positioning the Gallery at the same index it was positioned at before the orientation change. But then onItemSelected is called, on the centred image. This causes the centred image to be selected as their choice (green border etc), without the user clicking on it.

I have tried using a Boolean flag, "configIsChanging", to only execute the onItemSelected code when configIsChanging is false. Unfortunately, this doesn't seem to work, as onItemSelected is called after onConfigChange completes, and by that point the flag has been reset to false.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    configChanging = true; 

    tempConfigDisplayedIndex = mGallery.getSelectedItemPosition(); 

    super.onConfigurationChanged(newConfig);

    setContentView(R.layout.take_questionnaire);

    initialiseUI(); 
    configChanging = false; 
}

Has anyone had any experience with rogue onItemSelected events? Any ideas? I can post more code if required. Many thanks for any help.

Answers


have you tried just putting android:configChanges="orientation" in your manifest?


Need Your Help

Generating a unique machine id

windows winapi wmi uniqueidentifier

I need to write a function that generates an id that is unique for a given machine running a Windows OS.