1
+ /**
2
+ * Licensed under the Apache License, Version 2.0 (the "License");
3
+ * you may not use this file except in compliance with the License.
4
+ * You may obtain a copy of the License at
5
+ *
6
+ * http://www.apache.org/licenses/LICENSE-2.0
7
+ *
8
+ * Unless required by applicable law or agreed to in writing, software
9
+ * distributed under the License is distributed on an "AS IS" BASIS,
10
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ * See the License for the specific language governing permissions and
12
+ * limitations under the License.
13
+ */
14
+
15
+ package rx .android .samples ;
16
+
17
+ import android .os .Bundle ;
18
+ import android .util .Log ;
19
+ import android .widget .Button ;
20
+ import android .widget .Toast ;
21
+ import rx .Subscription ;
22
+ import rx .android .app .RxActivity ;
23
+ import rx .android .lifecycle .LifecycleObservable ;
24
+ import rx .android .view .OnClickEvent ;
25
+ import rx .android .view .ViewObservable ;
26
+ import rx .functions .Action1 ;
27
+
28
+ /**
29
+ * Simple example of creating a Subscription that is bound to the lifecycle
30
+ * (and thus automatically unsubscribed when the Activity is destroyed).
31
+ */
32
+ public class LifecycleObservableActivity extends RxActivity {
33
+
34
+ private static final String TAG = LifecycleObservableActivity .class .getSimpleName ();
35
+
36
+ private Button button ;
37
+
38
+ private Subscription subscription ;
39
+
40
+ @ Override
41
+ protected void onCreate (Bundle savedInstanceState ) {
42
+ super .onCreate (savedInstanceState );
43
+
44
+ button = new Button (this );
45
+ button .setText ("Click Me!" );
46
+ setContentView (button );
47
+ }
48
+
49
+ @ Override
50
+ protected void onStart () {
51
+ super .onStart ();
52
+
53
+ subscription =
54
+ LifecycleObservable .bindActivityLifecycle (lifecycle (), ViewObservable .clicks (button ))
55
+ .subscribe (new Action1 <OnClickEvent >() {
56
+ @ Override
57
+ public void call (OnClickEvent onClickEvent ) {
58
+ Toast .makeText (LifecycleObservableActivity .this ,
59
+ "Clicked button!" ,
60
+ Toast .LENGTH_SHORT )
61
+ .show ();
62
+ }
63
+ });
64
+ }
65
+
66
+ @ Override
67
+ protected void onPause () {
68
+ super .onPause ();
69
+
70
+ // Should output "false"
71
+ Log .i (TAG , "onPause(), isUnsubscribed=" + subscription .isUnsubscribed ());
72
+ }
73
+
74
+ @ Override
75
+ protected void onStop () {
76
+ super .onStop ();
77
+
78
+ // Should output "true"
79
+ Log .i (TAG , "onStop(), isUnsubscribed=" + subscription .isUnsubscribed ());
80
+ }
81
+ }
0 commit comments