Skip to content

Commit c22fc9a

Browse files
committed
Added sample Activity demonstrating LifecycleObservable
1 parent 26b8a37 commit c22fc9a

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

sample-app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ android {
3030

3131
dependencies {
3232
compile project(':rxandroid')
33+
compile project(':rxandroid-framework')
3334
}

sample-app/src/main/AndroidManifest.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@
5353
<action android:name="android.intent.action.MAIN"/>
5454
</intent-filter>
5555
</activity>
56+
57+
<activity android:name=".LifecycleObservableActivity"
58+
android:label="Rx Lifecycle">
59+
60+
<intent-filter>
61+
<category android:name="android.intent.category.LAUNCHER"/>
62+
<category android:name="android.intent.category.DEFAULT"/>
63+
<action android:name="android.intent.action.MAIN"/>
64+
</intent-filter>
65+
</activity>
66+
5667
</application>
5768

5869
</manifest>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)