title: Two-way Databinding with RxJava
body: <p>I am just learning about <code>RxJava</code> in Android and the (supposed) excellent composition of MVVM, databinding and RxJava. Unfortunately I cannot bind an <code>RxJava</code> <code>Observable</code> directly to a <code>View</code>: need a <code>LiveData</code>.</p>
  
  <p>So, I was wondering is there a way to implement Two-way databinding with RxJava?</p>
  
  <p>So far I've attempted to write a <code>BindingAdapter</code> which adds a listener to the passed <code>View</code> and calls <code>onNext</code> on the <code>Subject</code>.</p>
  
  <pre><code>@BindingAdapter("rxText")
  public static void bindReactiveText(EditText view, BehaviorSubject&lt;String&gt; text)
  {
      view.setText(text.getValue());
  
      view.addTextChangedListener(new TextWatcher() {
  
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
          }
  
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
          }
  
          @Override
          public void afterTextChanged(Editable s) {
              text.onNext(s.toString());
          }
      });
  }
  </code></pre>
  
  <p>What this does is updates the <code>Subject</code>/model with any changes in the <code>View</code>, so model always has consistent value with the <code>View</code>. However, the binding is never triggered for any change in the <code>Subject</code> itself (ofcourse we'd have to compare new and old values to stop from looping).</p>
  
  <p>Now, I did try to subscribe to the subject and call <code>setText</code> for each emission, but then I'd have to dispose the <code>Observer</code>. So what I did was also listened for View Attach State change: subscribe in <code>onViewAttachedToWindow</code> and dispose the observer in <code>onViewDetachedFromWindow</code>.</p>
  
  <pre><code>@BindingAdapter("rxText")
  public static void bindReactiveText(EditText editText, BehaviorSubject&lt;String&gt; text)
  {
      setText(editText, text.getValue());
  
      editText.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
          private TextWatcher textWatcher = new TextWatcher() {
  
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              }
  
              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
              }
  
              @Override
              public void afterTextChanged(Editable s) {
                  text.onNext(s.toString());
              }
          };
  
          private Disposable disposable;
  
          @Override
          public void onViewAttachedToWindow(View v) {
              editText.addTextChangedListener(textWatcher);
  
              disposable = text.subscribeOn(Schedulers.io())
                      .observeOn(AndroidSchedulers.mainThread())
                      .filter(s -&gt; !s.equals(editText.getText().toString()))
                      .subscribe(s -&gt; setText(editText, text.getValue()));
          }
  
          @Override
          public void onViewDetachedFromWindow(View v) {
              disposable.dispose();
              editText.removeTextChangedListener(textWatcher);
          }
      });
  }
  </code></pre>
  
  <p>And while that does work in the intended way, I'm not sure if this is the best approach to implement Two-way binding via RxJava.</p>
  
  <p>One of the immediate drawback that comes into mind is, <code>Activity</code>/<code>Fragment</code> cannot register a for a callback for most android <code>Views</code>. For instance, if I use the same approach for a Button and its click, and set up a listener in my <code>Activity</code> the binding will stop working.</p>
  
  <p>I am still learning RxJava and its various operators and their uses, so maybe I'm missing something obvious or committing a goof, but I've been trying to working out another way to do this for a few days now, so far have not been able to think of one.</p>
  
  <p><strong>So my question: What is the best approach to implementing Two-way data binding with RxJava <code>Observables</code>.</strong></p>