-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LifecycleObservable.bindActivityLifecycle without inheritance #152
Conversation
…yLifecycleCallbacks to listen for lifecycle events, instead of inheriting from RxJava
* @author Nabil Hachicha. | ||
*/ | ||
class LifecycleHelper implements Application.ActivityLifecycleCallbacks { | ||
private final Activity activityToMonitor; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've always thought it would be cool to have the Subject
output all events, then use Observable.filter()
to limit which ones you care about. Would get rid of all the ==
checks throughout the rest of this class.
Hi @dlew @JakeWharton
@Override
protected void onResume() {
subscription =
LifecycleObservable.bindActivityLifecycle(this,
ViewObservable.clicks(button),
LifecycleEvent.START)
.subscribe(new Action1<OnClickEvent>() {
@Override
public void call(OnClickEvent onClickEvent) {
Toast.makeText(LifecycleObservableActivityComposition.this,
"Clicked button!",
Toast.LENGTH_SHORT)
.show();
}
});
// LifecycleCallbacks will catch this event
super.onResume();
} In this case the first event is @Override
protected void onResume() {
// LifecycleCallbacks will not catch this event
super.onResume();
subscription =
LifecycleObservable.bindActivityLifecycle(this,
ViewObservable.clicks(button),
LifecycleEvent.START)
.subscribe(new Action1<OnClickEvent>() {
@Override
public void call(OnClickEvent onClickEvent) {
Toast.makeText(LifecycleObservableActivityComposition.this,
"Clicked button!",
Toast.LENGTH_SHORT)
.show();
}
});
} From a This is why the method accept this now as a parameter from the user (like
|
Observable | ||
.just(activity) | ||
.filter(sameInstance) | ||
.subscribe(sendCreateEvent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using an Observable
in all these callbacks is wasteful in both allocation and performance. I don't see any advantage over
if (activity == activityToMonitor) {
subscriber.onNext(LifecycleEvent.CREATE);
}
which is allocation-free.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spot on! changed to bindActivityUntilLifecycle
using LifecycleEvent event as the stopping event
@JakeWharton updated PR |
Ready/Waiting for review @JakeWharton @dlew :) |
As part of #172 LifecycleObservable has been removed from future releases. While something like it may come back (and not favoring inheritance) we are focused on making a 1.0 of a core that's fundamental to all users of RxJava on Android. |
A companion method to
LifecycleObservable.bindActivityLifecycle
that uses activity lifecycle callbacks provided in API 14 to avoid inheritance