I have a multiple buttons; When I press new button, previous(with another button) running request should be interrupted and new runs. How to realize it?
for (button : Buttons) {
button.setOnClickListener(b -> networkApi.getLongContentFromUrl(url)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<JsonElement>() {
@Override
public void onCompleted() {}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(JsonElement jsonElement) {
//do with result
}
}));
}
You can have a common
SerialSubscription
and assign your subscriber to it on button click. It will unsubscribe and thus cancel your previous stream: