Skip to content

Commit a85f8e6

Browse files
example using defer
1 parent 7fb98d1 commit a85f8e6

7 files changed

Lines changed: 132 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Learning RxJava2 for Android by example
1+
# Learning RxJava 2 for Android by example
22
## How to use RxJava 2 in Android Application
33
## How to migrate from RxJava 1 to RxJava 2
44

@@ -89,6 +89,7 @@ RxJava1 -> RxJava2
8989
* [ReplayExampleActivity](https://github.com/amitshekhariitbhu/RxJava2-Android-Samples/blob/master/app/src/main/java/com/rxjava2/android/samples/ReplayExampleActivity.java) - Using `replay` Operator
9090
* [ConcatExampleActivity](https://github.com/amitshekhariitbhu/RxJava2-Android-Samples/blob/master/app/src/main/java/com/rxjava2/android/samples/ConcatExampleActivity.java) - Using `concat` Operator
9191
* [MergeExampleActivity](https://github.com/amitshekhariitbhu/RxJava2-Android-Samples/blob/master/app/src/main/java/com/rxjava2/android/samples/MergeExampleActivity.java) - Using `merge` Operator
92+
* [DeferExampleActivity](https://github.com/amitshekhariitbhu/RxJava2-Android-Samples/blob/master/app/src/main/java/com/rxjava2/android/samples/DeferExampleActivity.java) - Using `defer` Observable
9293
* [IntervalExampleActivity](https://github.com/amitshekhariitbhu/RxJava2-Android-Samples/blob/master/app/src/main/java/com/rxjava2/android/samples/IntervalExampleActivity.java) - Using `Interval`
9394

9495
### TODO

‎app/src/main/AndroidManifest.xml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
<activity
6868
android:name=".MergeExampleActivity"
6969
android:label="@string/merge" />
70+
<activity
71+
android:name=".DeferExampleActivity"
72+
android:label="@string/defer" />
7073
</application>
7174

7275
</manifest>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.model.Car;
11+
12+
import io.reactivex.Observable;
13+
import io.reactivex.Observer;
14+
import io.reactivex.disposables.Disposable;
15+
16+
/**
17+
* Created by amitshekhar on 30/08/16.
18+
*/
19+
public class DeferExampleActivity extends AppCompatActivity {
20+
21+
private static final String TAG = SimpleExampleActivity.class.getSimpleName();
22+
Button btn;
23+
TextView textView;
24+
25+
@Override
26+
protected void onCreate(Bundle savedInstanceState) {
27+
super.onCreate(savedInstanceState);
28+
setContentView(R.layout.activity_example);
29+
btn = (Button) findViewById(R.id.btn);
30+
textView = (TextView) findViewById(R.id.textView);
31+
32+
btn.setOnClickListener(new View.OnClickListener() {
33+
@Override
34+
public void onClick(View view) {
35+
doSomeWork();
36+
}
37+
});
38+
}
39+
40+
/*
41+
* Defer used for Deferring Observable code until subscription in RxJava
42+
*/
43+
private void doSomeWork() {
44+
45+
Car car = new Car();
46+
47+
Observable<String> brandDeferObservable = car.brandDeferObservable();
48+
49+
car.setBrand("BMW"); // Even if we are setting the brand after creating Observable
50+
// we will get the brand as BMW.
51+
// If we had not used defer, we would have got null as the brand.
52+
53+
brandDeferObservable
54+
.subscribe(getObserver());
55+
}
56+
57+
private Observer<String> getObserver() {
58+
return new Observer<String>() {
59+
60+
@Override
61+
public void onSubscribe(Disposable d) {
62+
textView.append(" onSubscribe : isDisposed :" + d.isDisposed());
63+
Log.d(TAG, " onSubscribe : " + d.isDisposed());
64+
}
65+
66+
@Override
67+
public void onNext(String value) {
68+
textView.append(" onNext : value : " + value);
69+
Log.d(TAG, " onNext : value : " + value);
70+
}
71+
72+
@Override
73+
public void onError(Throwable e) {
74+
textView.append(" onError : " + e.getMessage());
75+
Log.d(TAG, " onError : " + e.getMessage());
76+
}
77+
78+
@Override
79+
public void onComplete() {
80+
textView.append(" onComplete");
81+
Log.d(TAG, " onComplete");
82+
}
83+
};
84+
}
85+
86+
87+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,8 @@ public void startConcatActivity(View view) {
8080
public void startMergeActivity(View view) {
8181
startActivity(new Intent(MainActivity.this, MergeExampleActivity.class));
8282
}
83+
84+
public void startDeferActivity(View view) {
85+
startActivity(new Intent(MainActivity.this, DeferExampleActivity.class));
86+
}
8387
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.rxjava2.android.samples.model;
2+
3+
import java.util.concurrent.Callable;
4+
5+
import io.reactivex.Observable;
6+
import io.reactivex.ObservableSource;
7+
8+
/**
9+
* Created by amitshekhar on 30/08/16.
10+
*/
11+
public class Car {
12+
13+
private String brand;
14+
15+
public void setBrand(String brand) {
16+
this.brand = brand;
17+
}
18+
19+
public Observable<String> brandDeferObservable() {
20+
return Observable.defer(new Callable<ObservableSource<? extends String>>() {
21+
@Override
22+
public ObservableSource<? extends String> call() throws Exception {
23+
return Observable.just(brand);
24+
}
25+
});
26+
}
27+
28+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,12 @@
133133
android:text="@string/merge"
134134
android:textColor="@android:color/black" />
135135

136+
<Button
137+
android:layout_width="match_parent"
138+
android:layout_height="wrap_content"
139+
android:onClick="startDeferActivity"
140+
android:text="@string/defer"
141+
android:textColor="@android:color/black" />
142+
136143
</LinearLayout>
137144
</ScrollView>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
<string name="replay">Replay</string>
1919
<string name="concat">Concat</string>
2020
<string name="merge">Merge</string>
21+
<string name="defer">Defer</string>
2122
</resources>

0 commit comments

Comments
 (0)