Skip to content

Commit c40d9ef

Browse files
committed
Move bindFragment call in sample to onCreate
Having it in onViewCreated is fine, but misleading, since we don't need to reattach retained fragments. (the referenced object remains the same.)
1 parent 0afafe3 commit c40d9ef

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/RetainedFragmentActivity.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ public void onCreate(Bundle savedInstanceState) {
6969
super.onCreate(savedInstanceState);
7070

7171
// simulate fetching a JSON document with a latency of 2 seconds
72-
strings = SampleObservables.fakeApiCall(2000).map(PARSE_JSON).cache();
72+
// in retained fragments, it's sufficient to bind the fragment in onCreate, since
73+
// Android takes care of detaching the Activity for us, and holding a reference for
74+
// the duration of the observable does not harm.
75+
strings = bindFragment(this, SampleObservables.fakeApiCall(2000).map(PARSE_JSON).cache());
7376
}
7477

7578
@Override
@@ -92,7 +95,7 @@ public void onViewCreated(final View view, Bundle savedInstanceState) {
9295

9396
// (re-)subscribe to the sequence, which either emits the cached result or simply re-
9497
// attaches the subscriber to wait for it to arrive
95-
subscription = bindFragment(this, strings).subscribe(new Action1<String>() {
98+
subscription = strings.subscribe(new Action1<String>() {
9699
@Override
97100
public void call(String result) {
98101
textView.setText(result);

0 commit comments

Comments
 (0)