Skip to content

Commit adb54b4

Browse files
committed
Add sample for fragments and connectable observables
1 parent 10fa255 commit adb54b4

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

rxjava-contrib/rxjava-android-samples/samples/src/main/AndroidManifest.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
<activity
1212
android:name=".RetainedFragmentActivity">
1313

14+
<intent-filter>
15+
<category android:name="android.intent.category.LAUNCHER"/>
16+
<category android:name="android.intent.category.DEFAULT"/>
17+
<action android:name="android.intent.action.MAIN"/>
18+
</intent-filter>
19+
</activity>
20+
<activity
21+
android:name=".ListeningFragmentActivity">
22+
1423
<intent-filter>
1524
<category android:name="android.intent.category.LAUNCHER"/>
1625
<category android:name="android.intent.category.DEFAULT"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.netflix.rxjava.android.samples;
2+
3+
import android.app.Activity;
4+
import android.app.Fragment;
5+
import android.os.Bundle;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.widget.TextView;
10+
import android.widget.Toast;
11+
12+
import rx.Subscriber;
13+
import rx.Subscription;
14+
import rx.observables.ConnectableObservable;
15+
import rx.subscriptions.Subscriptions;
16+
17+
import static rx.android.schedulers.AndroidSchedulers.mainThread;
18+
19+
/**
20+
* Problem:
21+
* You have a background sequence which keeps emitting items (either a limited or unlimited number)
22+
* and your UI component should be able to "listen in" to the sequence, i.e. it's okay to miss
23+
* in-flight items when going e.g. through a screen rotation or being otherwise detached from the
24+
* screen for a limited period of time. (Another example is a "page out" in a fragment ViewPager.)
25+
* <p/>
26+
* This is useful if you need behavior that mimics event buses. Think of a publishing
27+
* Observable as a channel or queue on an event bus.
28+
* <p/>
29+
* Solution:
30+
* Combine {@link android.app.Fragment#setRetainInstance(boolean)} with
31+
* {@link rx.android.schedulers.AndroidSchedulers#mainThread()} and {@link rx.Observable#publish()}
32+
*/
33+
public class ListeningFragmentActivity extends Activity {
34+
35+
@Override
36+
protected void onCreate(Bundle savedInstanceState) {
37+
super.onCreate(savedInstanceState);
38+
setContentView(R.layout.listening_fragment_activity);
39+
}
40+
41+
@SuppressWarnings("ConstantConditions")
42+
public static class ListeningFragment extends Fragment {
43+
44+
private ConnectableObservable<String> strings;
45+
private Subscription subscription = Subscriptions.empty();
46+
47+
public ListeningFragment() {
48+
setRetainInstance(true);
49+
}
50+
51+
@Override
52+
public void onCreate(Bundle savedInstanceState) {
53+
super.onCreate(savedInstanceState);
54+
55+
strings = SampleObservables.numberStrings(1, 50, 250).observeOn(mainThread()).publish();
56+
strings.connect(); // trigger the sequence
57+
}
58+
59+
@Override
60+
public void onDestroyView() {
61+
subscription.unsubscribe(); // stop listening
62+
super.onDestroyView();
63+
}
64+
65+
@Override
66+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
67+
return inflater.inflate(R.layout.retained_fragment, container, false);
68+
}
69+
70+
@Override
71+
public void onViewCreated(final View view, Bundle savedInstanceState) {
72+
super.onViewCreated(view, savedInstanceState);
73+
74+
final TextView textView = (TextView) view.findViewById(android.R.id.text1);
75+
76+
// re-connect to sequence
77+
subscription = strings.subscribe(new Subscriber<String>() {
78+
79+
@Override
80+
public void onCompleted() {
81+
Toast.makeText(getActivity(), "Done!", Toast.LENGTH_SHORT).show();
82+
}
83+
84+
@Override
85+
public void onError(Throwable throwable) {
86+
87+
}
88+
89+
@Override
90+
public void onNext(String s) {
91+
textView.setText(s);
92+
}
93+
});
94+
}
95+
}
96+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
tools:context="com.netflix.rxjava.android.samples.ListeningFragmentActivity">
6+
7+
<fragment
8+
android:tag="retained_fragment"
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent"
11+
android:name="com.netflix.rxjava.android.samples.ListeningFragmentActivity$ListeningFragment"
12+
tools:layout="@layout/retained_fragment" />
13+
14+
</RelativeLayout>

0 commit comments

Comments
 (0)