How to disable onItemSelectedListener to be invoked when setting selected item by code
android, listener, spinner
Solution
I have an easier, and I think, better solution. Since I had to refresh the spinners even after initialization, this is a more generic approach. Please refer the accepted answer:
Undesired onItemSelected calls
Problem
Just wondering how you handle the following problem: a result is calculated depending on two spinners' selected items. To handle the UI things, i.e. a user picks a new item in one of the spinners, I install a listener using `setOnItemSelectedListener` for the spinner in my `onCreate()` method of the activity. Now: that works, of course, fine. The listener's work is to trigger a new calculation of the result. The problem: because I intercept `onPause()` `onResume()` to save/restore the last state, I got a method that sets these two spinners' selected item programmatically like in here: ``` startSpinner.setSelection(pStart); destSpinner.setSelection(pDest); ``` These two calls invoke the listeners, too! My calculation method for the result plus the notification of a new result set is invoked twice here! A stupid direct approach for this would be to have a boolean variable disabling whatever the listener does inside, setting it before setting the selected items and resetting it afterwards. Okay. But is there a better method?? I don't want listeners to be called by code - actions, only by user actions! :-( How do you do it? Thanks!