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() {
lifecycleSubject.onNext(ActivityEvent.DESTROY);
super.onDestroy();
}

Depending on your use cases, you might need to do that for the rest of the lifecycle methods as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.157.142