I have a layout with three spinners. They differ in the option presented in the drop-down.
In my onCreateView
I have a method to setup the spinners. Inside that method I have something like this:
mySpinner = (Spinner) view.findViewById(R.id.my_spinner);
ArrayAdapter<String> mySpinner =
new ArrayAdapter<String>(getActivity(), R.layout.background,
new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.spinner_one_data))));
mySpinner.setDropDownViewResource(R.layout.spinner_text);
mySpinner.setAdapter(mySpinner);
mySpinner.setOnItemSelectedListener(this);
As I said, my other two spinners are almost the same but with different options.
I know that onItemSelected
is called once for every spinner in a "first setup" so I have a flag to prevent this problem. With this flag solution, my spinners are working as expected.
The problem is when I select in each spinner an option and then rotate the screen. Now, onItemSelected
is called 6 times instead the 3 times that I was expecting (I've set a flag to manage this situation of the 3 times calling).
Why Is it happening and hoe should I handle this?
I've found a solution that is working for me.
I have the
3
spinners soonItemSelected
is called3
times at the initial spinner setup. To avoidonItemSelected
from firing a method in the initial setup I've created a counter soonItemSelected
only fires the method accordingly the counter value.I've realized that in my situation, if a rotated the screen,
onItemSelected
is fired again the3
times, plus a time for each spinner that is not in the position0
.An example:
I have the
3
spinners and the user changes2
of them to one of the available option other then position0
so he ends up with a situation like this:Now, wen I rotate the screen,
onItemSelected
will be fired3
times for the initial spinner setup plus2
times for the spinners that aren't at position0
.I've saved the state in
onSaveInstanceState
and then, inonCreateView
I checked ifsavedInstanceState != null
and if so, extractedchangedSpinners
from the bundle and updated my counter to act accordingly.In general, I've found that there are many different events that can trigger the onItemSelected method, and it is difficult to keep track of all of them. Instead, I found it simpler to use an OnTouchListener to only respond to user-initiated changes.
Create your listener for the spinner:
Add the listener to the spinner as both an OnItemSelectedListener and an OnTouchListener:
To expand on Andres Q.'s answer... If you are using Java 8 you can do this with fewer lines of code by making use of lambda expressions. This method also forgoes the need to create a separate class in order to implement onTouchListener