Skip to content
This repository has been archived by the owner on Aug 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #11 from markus2610/master
Browse files Browse the repository at this point in the history
memory leak fix subscriber
  • Loading branch information
VictorAlbertos authored Jun 15, 2016
2 parents 5e47edb + 8d1968a commit 586011c
Showing 1 changed file with 7 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.subjects.PublishSubject;

public class RxActivityResult {
private static ActivitiesLifecycleCallbacks activitiesLifecycle;
Expand All @@ -49,7 +50,7 @@ public static <T extends Fragment> Builder<T> on(T fragment) {

public static class Builder<T> {
private final Class clazz;
private Subscriber<? super Result<T>> subscriber;
PublishSubject<Result<T>> subject = PublishSubject.create();
private final boolean uiTargetActivity;

public Builder(T t) {
Expand All @@ -75,11 +76,6 @@ public Observable<Result<T>> startIntent(final Intent intent) {
}

private Observable<Result<T>> startHolderActivity(Request request) {
Observable<Result<T>> observable = Observable.create(new Observable.OnSubscribe<Result<T>>() {
@Override public void call(Subscriber<? super Result<T>> aSubscriber) {
subscriber = aSubscriber;
}
});

OnResult onResult = uiTargetActivity ? onResultActivity() : onResultFragment();
request.setOnResult(onResult);
Expand All @@ -92,7 +88,7 @@ private Observable<Result<T>> startHolderActivity(Request request) {
}
});

return observable;
return subject.asObservable();
}

private OnResult onResultActivity() {
Expand All @@ -107,8 +103,8 @@ private OnResult onResultActivity() {
}

T activity = (T) activitiesLifecycle.getLiveActivity();
subscriber.onNext(new Result<T>((T) activity, resultCode, data));
subscriber.onCompleted();
subject.onNext(new Result<T>((T) activity, resultCode, data));
subject.onCompleted();
}
};
}
Expand All @@ -126,8 +122,8 @@ private OnResult onResultFragment() {
Fragment targetFragment = getTargetFragment(fragmentManager.getFragments());

if(targetFragment != null) {
subscriber.onNext(new Result<T>((T) targetFragment, resultCode, data));
subscriber.onCompleted();
subject.onNext(new Result<T>((T) targetFragment, resultCode, data));
subject.onCompleted();
}

//If code reaches this point it means some other activity has been stacked as a secondary process.
Expand Down

0 comments on commit 586011c

Please sign in to comment.