18
18
import static rx .android .schedulers .AndroidSchedulers .mainThread ;
19
19
20
20
import rx .Observable ;
21
+ import rx .functions .Func1 ;
21
22
import rx .operators .OperatorObserveFromAndroidComponent ;
22
23
import rx .operators .OperatorWeakBinding ;
23
24
@@ -40,7 +41,30 @@ public final class AndroidObservable {
40
41
USES_SUPPORT_FRAGMENTS = supportFragmentsAvailable ;
41
42
}
42
43
43
- private AndroidObservable () {}
44
+ private static final Func1 <Activity , Boolean > ACTIVITY_VALIDATOR = new Func1 <Activity , Boolean >() {
45
+ @ Override
46
+ public Boolean call (Activity activity ) {
47
+ return !activity .isFinishing ();
48
+ }
49
+ };
50
+
51
+ private static final Func1 <Fragment , Boolean > FRAGMENT_VALIDATOR = new Func1 <Fragment , Boolean >() {
52
+ @ Override
53
+ public Boolean call (Fragment fragment ) {
54
+ return fragment .isAdded ();
55
+ }
56
+ };
57
+
58
+ private static final Func1 <android .support .v4 .app .Fragment , Boolean > FRAGMENTV4_VALIDATOR =
59
+ new Func1 <android .support .v4 .app .Fragment , Boolean >() {
60
+ @ Override
61
+ public Boolean call (android .support .v4 .app .Fragment fragment ) {
62
+ return fragment .isAdded ();
63
+ }
64
+ };
65
+
66
+ private AndroidObservable () {
67
+ }
44
68
45
69
/**
46
70
* Transforms a source observable to be attached to the given Activity, in such a way that notifications will always
@@ -61,6 +85,7 @@ private AndroidObservable() {}
61
85
* @param sourceObservable the observable sequence to observe from the given Activity
62
86
* @param <T>
63
87
* @return a new observable sequence that will emit notifications on the main UI thread
88
+ * @deprecated Use {@link #bindActivity(android.app.Activity, rx.Observable)} instead
64
89
*/
65
90
@ Deprecated
66
91
public static <T > Observable <T > fromActivity (Activity activity , Observable <T > sourceObservable ) {
@@ -91,6 +116,7 @@ public static <T> Observable<T> fromActivity(Activity activity, Observable<T> so
91
116
* @param sourceObservable the observable sequence to observe from the given fragment
92
117
* @param <T>
93
118
* @return a new observable sequence that will emit notifications on the main UI thread
119
+ * @deprecated Use {@link #bindFragment(Object, rx.Observable)} instead
94
120
*/
95
121
@ Deprecated
96
122
public static <T > Observable <T > fromFragment (Object fragment , Observable <T > sourceObservable ) {
@@ -104,18 +130,38 @@ public static <T> Observable<T> fromFragment(Object fragment, Observable<T> sour
104
130
}
105
131
}
106
132
107
- public static <T > Observable <T > bindActivity (Activity activity , Observable <T > cachedSequence ) {
108
- return cachedSequence .observeOn (mainThread ()).lift (new OperatorWeakBinding <T , Activity >(activity ));
133
+ /**
134
+ * Binds the given source sequence to the life-cycle of an activity.
135
+ * <p/>
136
+ * This helper will schedule the given sequence to be observed on the main UI thread and ensure
137
+ * that no notifications will be forwarded to the activity in case it gets destroyed by the Android runtime
138
+ * or garbage collected by the VM.
139
+ *
140
+ * @param activity the activity to bind the source sequence to
141
+ * @param source the source sequence
142
+ */
143
+ public static <T > Observable <T > bindActivity (Activity activity , Observable <T > source ) {
144
+ return source .observeOn (mainThread ()).lift (new OperatorWeakBinding <T , Activity >(activity , ACTIVITY_VALIDATOR ));
109
145
}
110
146
147
+ /**
148
+ * Binds the given source sequence to the life-cycle of a fragment (native or support-v4).
149
+ * <p/>
150
+ * This helper will schedule the given sequence to be observed on the main UI thread and ensure
151
+ * that no notifications will be forwarded to the fragment in case it gets detached from its
152
+ * activity or garbage collected by the VM.
153
+ *
154
+ * @param fragment the fragment to bind the source sequence to
155
+ * @param source the source sequence
156
+ */
111
157
public static <T > Observable <T > bindFragment (Object fragment , Observable <T > cachedSequence ) {
112
158
Observable <T > source = cachedSequence .observeOn (mainThread ());
113
159
if (USES_SUPPORT_FRAGMENTS && fragment instanceof android .support .v4 .app .Fragment ) {
114
160
android .support .v4 .app .Fragment f = (android .support .v4 .app .Fragment ) fragment ;
115
- return source .lift (new OperatorWeakBinding <T , android .support .v4 .app .Fragment >(f ));
161
+ return source .lift (new OperatorWeakBinding <T , android .support .v4 .app .Fragment >(f , FRAGMENTV4_VALIDATOR ));
116
162
} else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .HONEYCOMB && fragment instanceof Fragment ) {
117
163
Fragment f = (Fragment ) fragment ;
118
- return source .lift (new OperatorWeakBinding <T , Fragment >(f ));
164
+ return source .lift (new OperatorWeakBinding <T , Fragment >(f , FRAGMENT_VALIDATOR ));
119
165
} else {
120
166
throw new IllegalArgumentException ("Target fragment is neither a native nor support library Fragment" );
121
167
}
0 commit comments