-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathRotationPersist1WorkerFragment.java
More file actions
81 lines (65 loc) · 2.31 KB
/
Copy pathRotationPersist1WorkerFragment.java
File metadata and controls
81 lines (65 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.morihacky.android.rxjava.fragments;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import com.morihacky.android.rxjava.MainActivity;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import io.reactivex.flowables.ConnectableFlowable;
import java.util.concurrent.TimeUnit;
public class RotationPersist1WorkerFragment extends Fragment {
public static final String TAG = RotationPersist1WorkerFragment.class.toString();
private IAmYourMaster _masterFrag;
private ConnectableFlowable<Integer> _storedIntsFlowable;
private Disposable _storedIntsDisposable;
/**
* Hold a reference to the activity -> caller fragment this way when the worker frag kicks off we
* can talk back to the master and send results
*/
@Override
public void onAttach(Context context) {
super.onAttach(context);
_masterFrag =
(RotationPersist1Fragment)
((MainActivity) context)
.getSupportFragmentManager()
.findFragmentByTag(RotationPersist1Fragment.TAG);
if (_masterFrag == null) {
throw new ClassCastException("We did not find a master who can understand us :(");
}
}
/** This method will only be called once when the retained Fragment is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Retain this fragment across configuration changes.
setRetainInstance(true);
if (_storedIntsFlowable != null) {
return;
}
Flowable<Integer> intsObservable =
Flowable.interval(1, TimeUnit.SECONDS).map(Long::intValue).take(20);
_storedIntsFlowable = intsObservable.publish();
_storedIntsDisposable = _storedIntsFlowable.connect();
}
/** The Worker fragment has started doing it's thing */
@Override
public void onResume() {
super.onResume();
_masterFrag.observeResults(_storedIntsFlowable);
}
@Override
public void onDestroy() {
super.onDestroy();
_storedIntsDisposable.dispose();
}
/** Set the callback to null so we don't accidentally leak the Activity instance. */
@Override
public void onDetach() {
super.onDetach();
_masterFrag = null;
}
public interface IAmYourMaster {
void observeResults(Flowable<Integer> intsObservable);
}
}