1

Now I'm aware that adding listeners, then setting fields would trigger the change. So I set my fields then add listeners.

Ive even added a boolean as it was being triggered, to avoid this. However after OnResume. All my listeners are fireing. Can someone please explain why, and how to stop it. Thanks.

Here is my code/work flow:

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

   spn1.setSelection(2);
   spn2.setSelection(15); // Gets replaced by the listener to 2, when it shouldnt!

   UseListeners = false; // Ignores fired events with an IF statement.

   addListeners();

   //UseListeners = true;
}

@Override
protected void onResume() {
    super.onResume();   
    UseListeners = true;
}


private void addListeners() {  
    spn1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            try{
                if(UseListeners){                       
                    spn2.setSelection(spn1.getSelectedItemPosition());                      
                }
            } catch (Exception e)
            {
            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {            
        }
    }); 
 }

Surely after OnResume, nothing is even changing so it shouldnt be firing. On resume literally just sets boolean to true. And it is the last state before the app is running so I'm not sure where and why its then triggering. Its Super is called before being allowed to use triggers too.

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • can we see the rest of the code? (set fields and add listeners) at least 1 example so we can see what you're doing. – Th0rndike Jul 03 '12 at 10:40
  • @Th0rndike There we go :) When it first tries to trigger (when creating the listeners), it skips due to boolean. But then triggers after `onResume();` at some point during activity initialising – IAmGroot Jul 03 '12 at 10:45
  • Do they fire if you remove onResume() method? – Alexander Jul 03 '12 at 10:57
  • @Alexander aka, removing `UseListeners = true;`? Nope. I see the desired values in my fields. (The listener triggers, but isn't granted permission to run/change anything) – IAmGroot Jul 03 '12 at 11:02
  • I mean removing the whole overridden onResume() method from your class, if there is nothing meaningful in this method. – Alexander Jul 03 '12 at 11:05
  • Having the overriden class is causing no change. Origionally I was setting the flag at the end of `onCreate()`. I overrid the method to ensure there wasnt something happening in `super.onResume()` or between that on and `onCreate()` – IAmGroot Jul 03 '12 at 11:09

1 Answers1

2

I think that listeners are invoked after layout is created. It's looks like creating view happens at some point after onPause() method, so insted of setting your boolean flag in onPause() you should set it in listener.

See here and here.

Community
  • 1
  • 1
Leszek
  • 6,568
  • 3
  • 42
  • 53