34

In an Android app (or Java more generally if it's no different), what is the best way of calling a method whenever a variable's value changes?

2
  • There must be some piece of code which changes the variable - Why don't you take the action there itself? Commented Aug 23, 2011 at 6:37
  • 11
    Because the object I need to access when the variable changes isn't in the scope of the code changing the variable. Commented Aug 23, 2011 at 6:41

2 Answers 2

63

What you really want to do is set up event-driven model to trigger a listener when an event happen (in your case, say a variable value has changed). This is very common not only for Java, but for other programming languages as well especially in the context of UI programming (though it is not necessarily only for that)

Typically this is done by doing the following steps:

  • Decide the interface that the listener should implement in the case of the event is triggered. For your case, you could call it VariableChangeListener and define the interface as:
 public interface VariableChangeListener { public void onVariableChanged(Object... variableThatHasChanged); } 

You could put any argument that you think it is important for the listener to handle here. By abstracting into the interface, you have flexibility of implementing the necessary action in the case of variable has changed without tight coupling it with the class where the event is occurring.

  • In the class where the event occurred (again in your case, the class where the variable could change), add a method to register a listener for the event. If you call your interface VariableChangeListener then you will have a method such as
 // while I only provide an example with one listener in this method, in many cases // you could have a List of Listeners which get triggered in order as the event // occurres public void setVariableChangeListener(VariableChangeListener variableChangeListener) { this.variableChangeListener = variableChangeListener; } 

By default there is no one listening to the event

  • In the case of event occurred (variable has changed), you will then trigger the listener, the code would look like something like
 if( variableValue != previousValue && this.variableChangeListener != null) { // call the listener here, note that we don't want to a strong coupling // between the listener and where the event is occurring. With this pattern // the code has the flexibility of assigning the listener this.variableChangeListener.onVariableChanged(variableValue); } 

Again this is a very common practice in programming to basically reacts to an event or variable change. In Javascript you see this is as part of onclick(), in Android you could check the event driven model for various listener such as OnClickListener set on a Button onclick event. In your case you will just trigger the listener based on different event which is whenever the variable has changed

Sign up to request clarification or add additional context in comments.

3 Comments

I think you may have finally helped the penny to drop for me on listeners with this explanation. This page is being bookmarked!!!
Could someone explain when the method setVariableChangeListener should be called? Since VariableChangeListener is an interface, what exactly gets passed into setVariableChangeListener?
Same question as AvP, also where does the if go? onCreate?
-6

You can make like this.As you have not defined what you want exactly.It may work. Suppose your variable name is v.Take one variable previous_v. Make previous_v=v; . You can put in your code according to your requirement:

if(v!=previous_v){ //your stuffs } 

1 Comment

This wouldn't work, because it would merely check the if once then never check again. The author wants to know WHEN the variable gets changed, like a background while loop that keeps checking.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.