title: Android: Listening for variable changes
body: <p>I've been searching for a KISS example of how to do this, and while they all seem (and I've gone through them ALL!) simple enough, I still cannot get my head around the concept.  I'm referring to custom listeners (ones that do not extend anything)...  In particular, creating a listener for a boolean variable so that I can set off some methods when its value changes.</p>
  
  <p>this is an example of an example I've tried:
  <a href="https://stackoverflow.com/questions/9879780/android-how-to-make-listener-to-a-custom-variable">android how to make listener to a custom variable?</a></p>
  
  <p>If someone has the time to explain a custom listener and how it works in plain English, that would be much appreciated.  I'm hoping that once I understand exactly how the listener is notified of a change, then I'll be able to create my own and understand how the damn thing works.  This is giving me a headache...  Thanks in advance</p>
  
  <p>Here is where I'm at at the moment:</p>
  
  <pre><code>public class ChangeListener {
      boolean boo = false;
  
      public ChangeListener(boolean b){
          boo = b;
      }
  
      private listener l = null;
  
      public interface listener{
          public void onChange(boolean b);
      }
  
      public void setChangeListener(listener mListener){
          l = mListener;
      }
  
      public void somethingChanged(){
          if(l != null){
              l.onChange(boo);
          }
      }
  
  
  }
  </code></pre>
  
  <p>This is a snapshot of my main activity:</p>
  
  <pre><code>public class Main extends Activity implements ChangeListener.listener{
      private ChangeListener listener;
      private boolean boo = false;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          listener = new ChangeListener(boo);
          listener.setChangeListener(this);
          listener.somethingChanged();
  
      }
  
      @Override
      public void onChange(boolean b) {
          // TODO Auto-generated method stub
          Toast.makeText(this, "REST = "+b, Toast.LENGTH_SHORT).show();
      }
  
  
  }
  </code></pre>
  
  <p>Now, when the activity starts, the Toast displays the state of boo... great.  Problem is this only happens within onCreate, which makes sense as .somethingChanged is placed in there.  What I would like is for the Toast to be generated every time boo is changed from false to true, and true to false regardless if its within onCreate or not.  I.e:</p>
  
  <ul>
  <li>Click a button</li>
  <li>Causes boo to change from false to true</li>
  <li>Listener picks up on the change and runs whatever is in onChange</li>
  </ul>
  
  <p>I know I could just use the button actionListener to do this, but I would like to stay away from this if possible, which at the moment I'm starting to think is impossible.</p>
  
  <p>Any insight would be much appreciated.  Thanks in advance!</p>