RxJava2: Passing Parameter To Observable.create Or fromCallable

Aug 25, 2017
Use final

There is no special function to pass parameters, just use final.

public Observable<String[]> splitWords(final String phrase) {    return Observable.fromCallable(new Callable<String[]>() {        @Override        public String[] call() throws Exception {            return phrase.split("\\s+");        }    });}
public Observable<String[]> splitWords(final String phrase) {    return Observable.create(new ObservableOnSubscribe<String[]>() {        @Override        public void subscribe(@NonNull ObservableEmitter<String[]> emitter) throws Exception {            emitter.onNext(phrase.split("\\s+"));            emitter.onComplete();        }    });}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.