|
| 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 | +} |
0 commit comments