Skip to content

Instantly share code, notes, and snippets.

@Takhion
Last active September 30, 2016 16:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Takhion/8b7514bdc7b82e914ea7a866871654b2 to your computer and use it in GitHub Desktop.
Save Takhion/8b7514bdc7b82e914ea7a866871654b2 to your computer and use it in GitHub Desktop.
Simplest way to retain an active Observable during configuration changes
package me.eugeniomarletti.example;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.Toast;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import java.util.concurrent.TimeUnit;
public class RetainedObservableActivity extends Activity {
@Nullable
private Observable<String> observable;
@Nullable
private Subscription subscription;
@NonNull
private static Observable<String> observableFactory() {
// just an example
return Observable
.interval(0, 1, TimeUnit.SECONDS)
.take(10)
.map(String::valueOf);
}
@NonNull
private static Observable<String> createCachedObservable() {
return observableFactory()
.observeOn(AndroidSchedulers.mainThread())
.replay(1)
.autoConnect();
}
@NonNull
private Observable<String> getOrCreateObservable() {
Observable<String> observable = this.observable;
if (observable == null) {
observable = createCachedObservable();
this.observable = observable;
}
return observable;
}
private void resetObservable() {
this.observable = null;
unsubscribe();
}
private void unsubscribe() {
if (subscription != null) {
subscription.unsubscribe();
subscription = null;
}
}
private void subscribe() {
if (subscription == null || subscription.isUnsubscribed()) {
subscription = getOrCreateObservable().subscribe(new Subscriber<String>() {
@Override
public void onNext(String s) {
Toast.makeText(RetainedObservableActivity.this, s, Toast.LENGTH_SHORT).show();
}
@Override
public void onCompleted() {
Toast.makeText(RetainedObservableActivity.this, "COMPLETED", Toast.LENGTH_SHORT).show();
resetObservable();
}
@Override
public void onError(Throwable e) {
Toast.makeText(RetainedObservableActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
resetObservable();
}
});
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
observable = getLastNonConfigurationInstance();
subscribe();
}
@Override
protected void onDestroy() {
super.onDestroy();
unsubscribe();
}
@Override
@SuppressWarnings("deprecation") // safe to use
public Object onRetainNonConfigurationInstance() {
return observable;
}
@Nullable
@Override
@SuppressWarnings("deprecation") // safe to use
public Observable<String> getLastNonConfigurationInstance() {
//noinspection unchecked // we always pass the same thing in this example
return (Observable<String>)super.getLastNonConfigurationInstance();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment