O'Reilly logo

Reactive Android Programming by Tadas Subonis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Binding to Activity without subclassing

However, it might not be always feasible to make your classes extend RxActivity. It happens quite often that your class already extends some other class that you cannot control.

In this case, the best approach would be to re-implement the RxLifecycle class (which is actually very light) by introducing a Subject (more about them later):

BehaviorSubject<ActivityEvent> lifecycleSubject = BehaviorSubject.create();

Also, we would have to override appropriate lifecycle methods, such as this:

void onCreate(@Nullable Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    lifecycleSubject.onNext(ActivityEvent.CREATE);}

Another example of an appropriate lifecycle method is this:

void onDestroy() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required