GeezerVison is showing github.com — Cancel Observable subscription when the Activity is "really" destroyed · f-evil/RxJava-Android-Samples@2947583
File tree
Expand file treeCollapse file tree
Open diff view settings
app/src/main/java/com/morihacky/android/rxjava
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: app/src/main/java/com/morihacky/android/rxjava/RotationPersistFragment.java -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -108,7 +108,6 @@ public void onNext(Integer integer) { |
108 | 108 | } |
109 | 109 | })); |
110 | 110 |
|
111 | | - intsObservable.connect(); |
112 | 111 | } |
113 | 112 |
|
114 | 113 | // ----------------------------------------------------------------------------------- |
|
Collapse file
Copy file name to clipboardExpand all lines: app/src/main/java/com/morihacky/android/rxjava/RotationPersistWorkerFragment.java +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
6 | 6 | import java.util.List; |
7 | 7 | import java.util.concurrent.TimeUnit; |
8 | 8 | import rx.Observable; |
| 9 | +import rx.Subscription; |
9 | 10 | import rx.functions.Func1; |
10 | 11 | import rx.observables.ConnectableObservable; |
11 | 12 |
|
@@ -14,6 +15,7 @@ public class RotationPersistWorkerFragment |
14 | 15 |
|
15 | 16 | private ConnectableObservable<Integer> storedIntsObservable; |
16 | 17 | private IAmYourMaster _masterFrag; |
| 18 | + private Subscription storedIntsSubscription; |
17 | 19 |
|
18 | 20 | /** |
19 | 21 | * Hold a reference to the activity -> caller fragment |
@@ -66,6 +68,8 @@ public Integer call(Long aLong) { |
66 | 68 | //_intsObservable = _intsObservable.share(); |
67 | 69 | storedIntsObservable = intsObservable.replay(); |
68 | 70 |
|
| 71 | + storedIntsSubscription = storedIntsObservable.connect(); |
| 72 | + |
69 | 73 | // Do not do this in production! |
70 | 74 | // `.share` is "warm" not "hot" |
71 | 75 | // the below forceful subscription fakes the heat |
@@ -91,6 +95,11 @@ public void onDetach() { |
91 | 95 | _masterFrag = null; |
92 | 96 | } |
93 | 97 |
|
| 98 | + @Override public void onDestroy() { |
| 99 | + super.onDestroy(); |
| 100 | + storedIntsSubscription.unsubscribe(); |
| 101 | + } |
| 102 | + |
94 | 103 | public interface IAmYourMaster { |
95 | 104 | void observeResults(ConnectableObservable<Integer> intsObservable); |
96 | 105 | } |
|