Skip to content

Commit f1c4226

Browse files
Add BehaviorSubjectExample
1 parent acf5bb6 commit f1c4226

6 files changed

Lines changed: 147 additions & 1 deletion

File tree

‎app/src/main/AndroidManifest.xml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585
<activity
8686
android:name=".PublishSubjectExample"
8787
android:label="@string/publish_subject" />
88+
<activity
89+
android:name=".BehaviorSubjectExample"
90+
android:label="@string/behavior_subject" />
8891
</application>
8992

9093
</manifest>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.rxjava2.android.samples;
2+
3+
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.util.Log;
6+
import android.view.View;
7+
import android.widget.Button;
8+
import android.widget.TextView;
9+
10+
import com.rxjava2.android.samples.utils.AppConstant;
11+
12+
import io.reactivex.Observer;
13+
import io.reactivex.disposables.Disposable;
14+
import io.reactivex.subjects.BehaviorSubject;
15+
16+
/**
17+
* Created by amitshekhar on 17/12/16.
18+
*/
19+
20+
public class BehaviorSubjectExample extends AppCompatActivity {
21+
22+
private static final String TAG = BehaviorSubjectExample.class.getSimpleName();
23+
Button btn;
24+
TextView textView;
25+
26+
@Override
27+
protected void onCreate(Bundle savedInstanceState) {
28+
super.onCreate(savedInstanceState);
29+
setContentView(R.layout.activity_example);
30+
btn = (Button) findViewById(R.id.btn);
31+
textView = (TextView) findViewById(R.id.textView);
32+
33+
btn.setOnClickListener(new View.OnClickListener() {
34+
@Override
35+
public void onClick(View view) {
36+
doSomeWork();
37+
}
38+
});
39+
}
40+
41+
/* When an observer subscribes to a BehaviorSubject, it begins by emitting the item most
42+
* recently emitted by the source Observable (or a seed/default value if none has yet been
43+
* emitted) and then continues to emit any other items emitted later by the source Observable(s).
44+
*/
45+
private void doSomeWork() {
46+
47+
BehaviorSubject<Integer> source = BehaviorSubject.create();
48+
49+
source.subscribe(getFirstObserver()); // it will get 1, 2, 3, 4 and onComplete
50+
51+
source.onNext(1);
52+
source.onNext(2);
53+
source.onNext(3);
54+
55+
/*
56+
* it will emit 3(last emitted), 4 and onComplete for second observer also.
57+
*/
58+
source.subscribe(getSecondObserver());
59+
60+
source.onNext(4);
61+
source.onComplete();
62+
63+
}
64+
65+
66+
private Observer<Integer> getFirstObserver() {
67+
return new Observer<Integer>() {
68+
69+
@Override
70+
public void onSubscribe(Disposable d) {
71+
Log.d(TAG, " First onSubscribe : " + d.isDisposed());
72+
}
73+
74+
@Override
75+
public void onNext(Integer value) {
76+
textView.append(" First onNext : value : " + value);
77+
textView.append(AppConstant.LINE_SEPARATOR);
78+
Log.d(TAG, " First onNext value : " + value);
79+
}
80+
81+
@Override
82+
public void onError(Throwable e) {
83+
textView.append(" First onError : " + e.getMessage());
84+
textView.append(AppConstant.LINE_SEPARATOR);
85+
Log.d(TAG, " First onError : " + e.getMessage());
86+
}
87+
88+
@Override
89+
public void onComplete() {
90+
textView.append(" First onComplete");
91+
textView.append(AppConstant.LINE_SEPARATOR);
92+
Log.d(TAG, " First onComplete");
93+
}
94+
};
95+
}
96+
97+
private Observer<Integer> getSecondObserver() {
98+
return new Observer<Integer>() {
99+
100+
@Override
101+
public void onSubscribe(Disposable d) {
102+
textView.append(" Second onSubscribe : isDisposed :" + d.isDisposed());
103+
Log.d(TAG, " Second onSubscribe : " + d.isDisposed());
104+
textView.append(AppConstant.LINE_SEPARATOR);
105+
}
106+
107+
@Override
108+
public void onNext(Integer value) {
109+
textView.append(" Second onNext : value : " + value);
110+
textView.append(AppConstant.LINE_SEPARATOR);
111+
Log.d(TAG, " Second onNext value : " + value);
112+
}
113+
114+
@Override
115+
public void onError(Throwable e) {
116+
textView.append(" Second onError : " + e.getMessage());
117+
textView.append(AppConstant.LINE_SEPARATOR);
118+
Log.d(TAG, " Second onError : " + e.getMessage());
119+
}
120+
121+
@Override
122+
public void onComplete() {
123+
textView.append(" Second onComplete");
124+
textView.append(AppConstant.LINE_SEPARATOR);
125+
Log.d(TAG, " Second onComplete");
126+
}
127+
};
128+
}
129+
130+
131+
}

‎app/src/main/java/com/rxjava2/android/samples/MainActivity.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,8 @@ public void startReplaySubjectActivity(View view) {
104104
public void startPublishSubjectActivity(View view) {
105105
startActivity(new Intent(MainActivity.this, PublishSubjectExample.class));
106106
}
107+
108+
public void startBehaviorSubjectActivity(View view) {
109+
startActivity(new Intent(MainActivity.this, BehaviorSubjectExample.class));
110+
}
107111
}

‎app/src/main/java/com/rxjava2/android/samples/PublishSubjectExample.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private void doSomeWork() {
5252
source.onNext(3);
5353

5454
/*
55-
* it will emit 4 and onComplete for second observer also as we have used replay
55+
* it will emit 4 and onComplete for second observer also.
5656
*/
5757
source.subscribe(getSecondObserver());
5858

‎app/src/main/res/layout/activity_main.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,12 @@
175175
android:onClick="startPublishSubjectActivity"
176176
android:text="@string/publish_subject"
177177
android:textColor="@android:color/black" />
178+
179+
<Button
180+
android:layout_width="match_parent"
181+
android:layout_height="wrap_content"
182+
android:onClick="startBehaviorSubjectActivity"
183+
android:text="@string/behavior_subject"
184+
android:textColor="@android:color/black" />
178185
</LinearLayout>
179186
</ScrollView>

‎app/src/main/res/values/strings.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@
2424
<string name="last">Last</string>
2525
<string name="replay_subject">ReplaySubject</string>
2626
<string name="publish_subject">PublishSubject</string>
27+
<string name="behavior_subject">BehaviorSubject</string>
2728
</resources>

0 commit comments

Comments
 (0)