android – AlertDialog with RxJava

Question:

Using AsyncTask when processing an array of data, we can easily display the progress of processing using AlertDialog.

Can this be done with RxJava? I ask for your simplest example.

And how is the problem of rotating the device solved when downloading data from the server using RxJava and displaying the progress on the AlertDialog?

Answer:

The easiest way is to use the from method:

    List<String> list = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        list.add("item "+i);
    }

    Observable.from(list)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<String>() {
        @Override
        public void onCompleted() {
            if(progress!=null && progress.isShowing()) progress.dismiss();
        }

        @Override
        public void onError(Throwable e) {
            if(progress!=null && progress.isShowing()) progress.dismiss();
        }

        @Override
        public void onNext(String s) {
            progress =  ProgressDialog.show(this, "dialog title",
                    "dialog message", true);
        }
    });
Scroll to Top