Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing rapid clicks with RXJava

Tags:

rx-java

I am writing an android app and using rxjava to handle user input events. Basically what I want to do is, emit when a button is clicked, and then drop subsequent emissions for some period of time afterwards (like a second or two), essentially to prevent having to process multiple clicks of the button.

like image 382
offbyone Avatar asked Mar 05 '14 17:03

offbyone


3 Answers

I think throttleFirst is what you want: https://github.com/Netflix/RxJava/wiki/Filtering-Observables#wiki-throttlefirst

like image 54
zsxwing Avatar answered Oct 22 '22 19:10

zsxwing


For preventing fast clicks i use this code

RxView.clicks(your_view)
            .throttleFirst(300, TimeUnit.MILLISECONDS)
            .subscribe {
                //on click
            }
like image 37
Radesh Avatar answered Oct 22 '22 19:10

Radesh


Continuing zsxwing's Answer:

If you're not using RxBinding library but only RxJava then,

io.reactivex.Observable.just(view_obj)
                .throttleFirst(1, TimeUnit.SECONDS)// prevent rapid click for 1 seconds
                .blockingSubscribe(o -> {                     
                    startActivity(...);
                });
like image 2
Aks4125 Avatar answered Oct 22 '22 18:10

Aks4125