Get current RxJava Observable value

In my user interface I have RadioGroupwith 3 RadioButtonsand "normal" Button.

When the user clicks on Button, I want to read the selected RadioButtonone to do something with it.

RxView.clicks(button)
      .flatMap(x -> RxRadioGroup.checkedChanges(radioGroup_timer))
      .map(view -> {
          switch (view) {
              case R.id.radioButton_10sec_timer:
                  return 10;
              case R.id.radioButton_20sec_timer:
                  return 20;
              default:
                  return DEFAULT_TIMER;
          }
      })
    .subscribe(duration -> Timber.i("duration: %,d",duration));

It still works, but now every time the user selects another RadioButton, I get a new log message.

Is there a way to read the current selection RadioButtonwithout subscribing to the changes?

+4
source share
1 answer

Do something like that

RxView.clicks(button)
      .withLatestFrom(RxRadioGroup.checkedChanges(radioGroup_timer), (click, checked) -> checked)
      .map(...)

withLatestFrom LATEST checkedChanges, flatMap Observable,

+3

Source: https://habr.com/ru/post/1655822/


All Articles