1

I have an int variable called mCurrentIndex. I want to do something when its value changes.

For example:

public class MainActivity extends Activity{

private int mCurrentIndex = 0;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Apps logic.
  }

   public onCurrentIndexValueChange(){

   button.setClickable(true);

   }
}
D Ta
  • 864
  • 6
  • 17
Abdelaziz Faki
  • 27
  • 1
  • 10
  • 1
    Possible duplicate of [Android execute code on variable change](https://stackoverflow.com/questions/9387000/android-execute-code-on-variable-change) – Ali Ahsan Jan 14 '19 at 17:23
  • Possible duplicate of [How to create change listener for variable?](https://stackoverflow.com/questions/15433855/how-to-create-change-listener-for-variable) – Dave S Jan 14 '19 at 17:33
  • android.databinding.ObservableInt is an implementation of the observer pattern which others have noted. –  Jan 14 '19 at 17:37

3 Answers3

7

One approach to solving this is using Android's LiveData.

In your situation, since you'd like to observe an int, you can do something like this:

public MutableLiveData<Integer> mCurrentIndex = new MutableLiveData<>();

To change your value, you would do this:

mCurrentIndex.setValue(12345); // Replace 12345 with your int value.

To observe the changes you would do the following:

mCurrentIndex.observe(this, new Observer<Integer>() {
        @Override
        public void onChanged(@Nullable final Integer newIntValue) {
            // Update the UI, in this case, a TextView.
            mNameTextView.setText("My new current index is: " + newIntValue);
        }
    };);
}

This approach is useful because you can define a ViewModel to segregate your logic from your Activity while having the UI observe and reflect the changes that occur.

By separating your logic out to the ViewModel, your logic becomes more easily testable since writing tests for your ViewModel is relatively easier than writing tests for your Activity.

For more information on LiveData check out this link.

D Ta
  • 864
  • 6
  • 17
  • Thank you for sharing this info, i have a question in mind , so how is it different from a listener ? and does this approach works well with MVC design pattern as well? – Abdelaziz Faki Jan 15 '19 at 18:47
  • You can technically use LiveData anywhere in your app, so you don't necessarily have to use it with MVVM. However, there are benefits to using it with Android's ViewModel class. Take a look at the LiveData documentation I linked and look at the "The advantages of using LiveData" section. – D Ta Jan 15 '19 at 22:59
1

you can use a setter function

 private int mCurrentIndex = 1;
 public void setCurrentIndex(int newValueFormCurrentIndex)
 {
   if(newValueFormCurrentIndex != mCurrentIndex)
    {
     onCurrentIndexValueChange();
     mCurrentIndex = newValueFormCurrentIndex;
    }
 }
Mouad Khali
  • 114
  • 3
0

If the scope on mCurrentIndex is only in MainActivity then I think it would be best just to create getter and setter methods for mCurrentIndex and at the end of the setter method call onCurrentIndexValueChange()

public int getIndex(){
  return this.mCurrentIndex;
}

public void setIndex(int value){
  this.mCurrentIndex = value;
  this.onCurrentIndexValueChange();
}
J Burk
  • 11
  • 3