3

I have Observable

Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
        @Override
        public void call(Subscriber<? super String> subscriber) {
            if (subscriber.isUnsubscribed()) {
                return;
            }

            for (int i = 0; i < 100; i++) {
               Thread.sleep(100);
              subscriber.onNext("Loading:"+i);
            }
            subscriber.onCompleted();
        }
    });

Subscriber

 Subscriber<? super String> sub = new Subscriber<String>() {
                @Override
                public void onCompleted() {
                    pDialog.setMessage("Successfully Done!");
                    pDialog.cancel();
                }

            @Override
            public void onError(Throwable e) {
                pDialog.cancel();
            }

            @Override
            public void onNext(String string) {
                pDialog.setMessage(string);
            }
        };

After click on button I do this:

  compositeSubscription.add(observable.subscribeOn(Schedulers.newThread()).observeOn(
            AndroidSchedulers.mainThread()).subscribe(sub));

In Activity:

@Override
protected void onStart() {
    super.onStart();
   compositeSubscription = new CompositeSubscription();
}

 @Override
    protected void onStop() {
        if (!compositeSubscription.isUnsubscribed()&&compositeSubscription.hasSubscriptions())
            compositeSubscription.unsubscribe();
        super.onStop();
    }

After clicking on Button, dialog is shown and message is updating.

BUT If I minimize the app and open it again Subscription is lost and message doesn't update, but process continue execute.

How to subscribe again to Observable without starting new process?

NickUnuchek
  • 11,794
  • 12
  • 98
  • 138

2 Answers2

2

1) You're dealing with a cold observable here, which means each subscription calls OnSubscribe.call() each time you subscribe. You can use publish() here which converts Observable to ConnectableObservable or any other operator converting cold to hot.

2) If you want your Observable to keep working after returning from background maybe onStart/onStop are not the correct lifecycle callbacks. I'd go for a onCreate/onDestroy there

3) Unsubscribing won't stop OnSubscribe.call() execution. The easiest solutiion is to check isUnsubscribed() inside your loop and stop accordingly.

krp
  • 2,247
  • 17
  • 14
0

Use a Subject:

Observable<Long> observable = Observable.interval(100, TimeUnit.MILLISECONDS);

PublishSubject<Object> subject = PublishSubject.create();
observable.subscribe(subject);

Subscription subscription = subject.subscribe(o -> {
    System.out.println("o = " + o);
});

Thread.sleep(500);
subscription.unsubscribe();

Subscription subscription2 = subject.subscribe(o -> {
    System.out.println("continued = " + o);
});

Thread.sleep(500);
subscription.unsubscribe();

This will produce the following output:

o = 0
o = 1
o = 2
o = 3
o = 4
continued = 5
continued = 6
continued = 7
continued = 8
continued = 9
Entea
  • 947
  • 14
  • 26