File tree Expand file tree Collapse file tree 5 files changed +67
-2
lines changed
app/src/main/java/com/morihacky/android/rxjava Expand file tree Collapse file tree 5 files changed +67
-2
lines changed Original file line number Diff line number Diff line change 33import android .os .Bundle ;
44import android .support .v4 .app .FragmentActivity ;
55import com .morihacky .android .rxjava .app .R ;
6+ import com .morihacky .android .rxjava .rxbus .RxBus ;
67import com .morihacky .android .rxjava .rxbus .RxBusDemoFragment ;
78import timber .log .Timber ;
89
910public class MainActivity
1011 extends FragmentActivity {
1112
13+ private RxBus _rxBus = null ;
14+
1215 @ Override
1316 protected void onCreate (Bundle savedInstanceState ) {
1417 super .onCreate (savedInstanceState );
@@ -18,7 +21,15 @@ protected void onCreate(Bundle savedInstanceState) {
1821
1922 getSupportFragmentManager ().beginTransaction ().addToBackStack (this .toString ())
2023 //.replace(R.id.activity_main, new MainFragment(), this.toString())
21- .replace (R .id .activity_main , new RxBusDemoFragment (), this .toString ())
22- .commit ();
24+ .replace (R .id .activity_main , new RxBusDemoFragment (), this .toString ()).commit ();
25+ }
26+
27+ // This is better done with a DI Library like Dagger
28+ public RxBus getRxBusSingleton () {
29+ if (_rxBus == null ) {
30+ _rxBus = new RxBus ();
31+ }
32+
33+ return _rxBus ;
2334 }
2435}
Original file line number Diff line number Diff line change 1+ package com .morihacky .android .rxjava .rxbus ;
2+
3+ import rx .Observable ;
4+ import rx .subjects .PublishSubject ;
5+ import rx .subjects .SerializedSubject ;
6+ import rx .subjects .Subject ;
7+
8+ /**
9+ * courtesy: https://gist.github.com/benjchristensen/04eef9ca0851f3a5d7bf
10+ */
11+ public class RxBus {
12+
13+ //private final PublishSubject<Object> _bus = PublishSubject.create();
14+
15+ // If multiple threads are going to emit events to this
16+ // then it must be made thread-safe like this instead
17+ private final Subject <Object , Object > _bus = new SerializedSubject <>(PublishSubject .create ());
18+
19+ public void send (Object o ) {
20+ _bus .onNext (o );
21+ }
22+
23+ public Observable <Object > toObserverable () {
24+ return _bus ;
25+ }
26+ }
27+
Original file line number Diff line number Diff line change 77import android .view .View ;
88import android .view .ViewGroup ;
99import butterknife .ButterKnife ;
10+ import com .morihacky .android .rxjava .MainActivity ;
1011import com .morihacky .android .rxjava .app .R ;
1112
1213public class RxBusFrag1
1314 extends Fragment {
1415
16+ private RxBus _rxBus ;
17+
1518 @ Override
1619 public View onCreateView (LayoutInflater inflater ,
1720 @ Nullable ViewGroup container ,
@@ -20,4 +23,10 @@ public View onCreateView(LayoutInflater inflater,
2023 ButterKnife .inject (this , layout );
2124 return layout ;
2225 }
26+
27+ @ Override
28+ public void onActivityCreated (@ Nullable Bundle savedInstanceState ) {
29+ super .onActivityCreated (savedInstanceState );
30+ _rxBus = ((MainActivity ) getActivity ()).getRxBusSingleton ();
31+ }
2332}
Original file line number Diff line number Diff line change 77import android .view .View ;
88import android .view .ViewGroup ;
99import butterknife .ButterKnife ;
10+ import com .morihacky .android .rxjava .MainActivity ;
1011import com .morihacky .android .rxjava .app .R ;
1112
1213public class RxBusFrag2
1314 extends Fragment {
1415
16+ private RxBus _rxBus ;
17+
1518 @ Override
1619 public View onCreateView (LayoutInflater inflater ,
1720 @ Nullable ViewGroup container ,
@@ -20,4 +23,10 @@ public View onCreateView(LayoutInflater inflater,
2023 ButterKnife .inject (this , layout );
2124 return layout ;
2225 }
26+
27+ @ Override
28+ public void onActivityCreated (@ Nullable Bundle savedInstanceState ) {
29+ super .onActivityCreated (savedInstanceState );
30+ _rxBus = ((MainActivity ) getActivity ()).getRxBusSingleton ();
31+ }
2332}
Original file line number Diff line number Diff line change 77import android .view .View ;
88import android .view .ViewGroup ;
99import butterknife .ButterKnife ;
10+ import com .morihacky .android .rxjava .MainActivity ;
1011import com .morihacky .android .rxjava .app .R ;
1112
1213public class RxBusFrag3
1314 extends Fragment {
1415
16+ private RxBus _rxBus ;
17+
1518 @ Override
1619 public View onCreateView (LayoutInflater inflater ,
1720 @ Nullable ViewGroup container ,
@@ -20,4 +23,10 @@ public View onCreateView(LayoutInflater inflater,
2023 ButterKnife .inject (this , layout );
2124 return layout ;
2225 }
26+
27+ @ Override
28+ public void onActivityCreated (@ Nullable Bundle savedInstanceState ) {
29+ super .onActivityCreated (savedInstanceState );
30+ _rxBus = ((MainActivity ) getActivity ()).getRxBusSingleton ();
31+ }
2332}
You can’t perform that action at this time.
0 commit comments