Android: Listening for variable changes

后端 未结 2 575
情书的邮戳
情书的邮戳 2020-12-08 13:30

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 co

相关标签:
2条回答
  • 2020-12-08 14:14

    There is a much easier solution now. Just wrap your variable in a LiveData Specifically MutableLiveData.

    Java Version

    MutableLiveData<String> listen = new MutableLiveData<>();
    
    listen.setValue("Changed value"); //Initilize with a value
    
    listen.observe(context, new Observer  > () {
        @Override
        public void onChanged(String changedValue) {
            //Do something with the changed value
        }
    });
    

    Kotlin Version

    val listen : MutableLiveData<String> =  MutableLiveData<>()
    
    listen.setValue("Changed value") //Initilize with a value
    
    listen.observe(context, Observer {
    
        //Do something with the changed value -> it
    
    })
    
    0 讨论(0)
  • 2020-12-08 14:17

    You wrap the variable up inside a custom class such that the only access functions also trip the listener. So that the only way to flip the switch is via the functions which toggles and calls the listener.

    public class BooVariable {
        private boolean boo = false;
        private ChangeListener listener;
    
        public boolean isBoo() {
            return boo;
        }
    
        public void setBoo(boolean boo) {
            this.boo = boo;
            if (listener != null) listener.onChange();
        }
    
        public ChangeListener getListener() {
            return listener;
        }
    
        public void setListener(ChangeListener listener) {
            this.listener = listener;
        }
    
        public interface ChangeListener {
            void onChange();
        }
    }
    

    To monitor the change you need to implement BooVariable.ChangeListener and then pass the BooVariable class a copy of "this" then, when you change the variable it calls onChange.

    Also, keeping in mind you can just inline the code rather than extend directly:

    BooVariable bv = new BooVariable();
    bv.setListener(new BooVariable.ChangeListener() {
        @Override
        public void onChange() {
            Toast.makeText(MainActivity.this,"blah", Toast.LENGTH_LONG).show();
         }
    });
    

    PS. The toast must be called from the UI thread, so you need to switch the Looper if you're going to change the variable in a different thread. This likely won't come up though.

    0 讨论(0)
提交回复
热议问题