Skip to content

Commit c146374

Browse files
ahmedreakarnokd
authored andcommitted
Update RxJava Android Module documentation (#6134)
This patch updates the RxJava Android wiki page to point to the RxAndroid project and the RxAndroid wiki page. This refs #6132.
1 parent c7f3349 commit c146374

File tree

1 file changed

+2
-109
lines changed

1 file changed

+2
-109
lines changed

Diff for: docs/The-RxJava-Android-Module.md

+2-109
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,3 @@
1-
**Note:** This page is out-of-date. See [the RxAndroid wiki](https://github.com/ReactiveX/RxAndroid/wiki) for more up-to-date instructions.
1+
## RxAndroid
22

3-
***
4-
5-
The `rxjava-android` module contains Android-specific bindings for RxJava. It adds a number of classes to RxJava to assist in writing reactive components in Android applications.
6-
7-
- It provides a `Scheduler` that schedules an `Observable` on a given Android `Handler` thread, particularly the main UI thread.
8-
- It provides operators that make it easier to deal with `Fragment` and `Activity` life-cycle callbacks.
9-
- It provides wrappers for various Android messaging and notification components so that they can be lifted into an Rx call chain
10-
- It provides reusable, self-contained, reactive components for common Android use cases and UI concerns. _(coming soon)_
11-
12-
# Binaries
13-
14-
You can find binaries and dependency information for Maven, Ivy, Gradle and others at [http://search.maven.org](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22rxandroid%22).
15-
16-
Here is an example for [Maven](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22rxandroid%22):
17-
18-
```xml
19-
<dependency>
20-
<groupId>io.reactivex</groupId>
21-
<artifactId>rxandroid</artifactId>
22-
<version>0.23.0</version>
23-
</dependency>
24-
```
25-
26-
&hellip;and for Ivy:
27-
28-
```xml
29-
<dependency org="io.reactivex" name="rxandroid" rev="0.23.0" />
30-
```
31-
32-
The currently supported `minSdkVersion` is `10` (Android 2.3/Gingerbread)
33-
34-
# Examples
35-
36-
## Observing on the UI thread
37-
38-
You commonly deal with asynchronous tasks on Android by observing the task&#8217;s result or outcome on the main UI thread. Using vanilla Android, you would typically accomplish this with an `AsyncTask`. With RxJava you would instead declare your `Observable` to be observed on the main thread by using the `observeOn` operator:
39-
40-
```java
41-
public class ReactiveFragment extends Fragment {
42-
43-
@Override
44-
public void onCreate(Bundle savedInstanceState) {
45-
super.onCreate(savedInstanceState);
46-
Observable.from("one", "two", "three", "four", "five")
47-
.subscribeOn(Schedulers.newThread())
48-
.observeOn(AndroidSchedulers.mainThread())
49-
.subscribe(/* an Observer */);
50-
}
51-
```
52-
53-
This executes the Observable on a new thread, which emits results through `onNext` on the main UI thread.
54-
55-
## Observing on arbitrary threads
56-
The previous example is a specialization of a more general concept: binding asynchronous communication to an Android message loop by using the `Handler` class. In order to observe an `Observable` on an arbitrary thread, create a `Handler` bound to that thread and use the `AndroidSchedulers.handlerThread` scheduler:
57-
58-
```java
59-
new Thread(new Runnable() {
60-
@Override
61-
public void run() {
62-
final Handler handler = new Handler(); // bound to this thread
63-
Observable.from("one", "two", "three", "four", "five")
64-
.subscribeOn(Schedulers.newThread())
65-
.observeOn(AndroidSchedulers.handlerThread(handler))
66-
.subscribe(/* an Observer */)
67-
68-
// perform work, ...
69-
}
70-
}, "custom-thread-1").start();
71-
```
72-
73-
This executes the Observable on a new thread and emits results through `onNext` on `custom-thread-1`. (This example is contrived since you could as well call `observeOn(Schedulers.currentThread())` but it illustrates the idea.)
74-
75-
## Fragment and Activity life-cycle
76-
77-
On Android it is tricky for asynchronous actions to access framework objects in their callbacks. That&#8217;s because Android may decide to destroy an `Activity`, for instance, while a background thread is still running. The thread will attempt to access views on the now dead `Activity`, which results in a crash. (This will also create a memory leak, since your background thread holds on to the `Activity` even though it&#8217;s not visible anymore.)
78-
79-
This is still a concern when using RxJava on Android, but you can deal with the problem in a more elegant way by using `Subscription`s and a number of Observable operators. In general, when you run an `Observable` inside an `Activity` that subscribes to the result (either directly or through an inner class), you must unsubscribe from the sequence in `onDestroy`, as shown in the following example:
80-
81-
```java
82-
// MyActivity
83-
private Subscription subscription;
84-
85-
protected void onCreate(Bundle savedInstanceState) {
86-
this.subscription = observable.subscribe(this);
87-
}
88-
89-
...
90-
91-
protected void onDestroy() {
92-
this.subscription.unsubscribe();
93-
super.onDestroy();
94-
}
95-
```
96-
97-
This ensures that all references to the subscriber (the `Activity`) will be released as soon as possible, and no more notifications will arrive at the subscriber through `onNext`.
98-
99-
One problem with this is that if the `Activity` is destroyed because of a change in screen orientation, the Observable will fire again in `onCreate`. You can prevent this by using the `cache` or `replay` Observable operators, while making sure the Observable somehow survives the `Activity` life-cycle (for instance, by storing it in a global cache, in a Fragment, etc.) You can use either operator to ensure that when the subscriber subscribes to an Observable that&#8217;s already &ldquo;running,&rdquo; items emitted by the Observable during the span when it was detached from the `Activity` will be &ldquo;played back,&rdquo; and any in-flight notifications from the Observable will be delivered as usual.
100-
101-
# See also
102-
* [How the New York Times is building its Android app with Groovy/RxJava](http://open.blogs.nytimes.com/2014/08/18/getting-groovy-with-reactive-android/?_php=true&_type=blogs&_php=true&_type=blogs&_r=1&) by Mohit Pandey
103-
* [Functional Reactive Programming on Android With RxJava](http://mttkay.github.io/blog/2013/08/25/functional-reactive-programming-on-android-with-rxjava/) and [Conquering concurrency - bringing the Reactive Extensions to the Android platform](https://speakerdeck.com/mttkay/conquering-concurrency-bringing-the-reactive-extensions-to-the-android-platform) by Matthias Käppler
104-
* [Learning RxJava for Android by example](https://github.com/kaushikgopal/Android-RxJava) by Kaushik Gopal
105-
* [Top 7 Tips for RxJava on Android](http://blog.futurice.com/top-7-tips-for-rxjava-on-android) and [Rx Architectures in Android](http://www.slideshare.net/TimoTuominen1/rxjava-architectures-on-android-8-android-livecode-32531688) by Timo Tuominen
106-
* [FRP on Android](http://slid.es/yaroslavheriatovych/frponandroid) by Yaroslav Heriatovych
107-
* [Rx for .NET and RxJava for Android](http://blog.futurice.com/tech-pick-of-the-week-rx-for-net-and-rxjava-for-android) by Olli Salonen
108-
* [RxJava in Xtend for Android](http://blog.futurice.com/android-development-has-its-own-swift) by Andre Medeiros
109-
* [RxJava and Xtend](http://mnmlst-dvlpr.blogspot.de/2014/07/rxjava-and-xtend.html) by Stefan Oehme
110-
* Grokking RxJava, [Part 1: The Basics](http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/), [Part 2: Operator, Operator](http://blog.danlew.net/2014/09/22/grokking-rxjava-part-2/), [Part 3: Reactive with Benefits](http://blog.danlew.net/2014/09/30/grokking-rxjava-part-3/), [Part 4: Reactive Android](http://blog.danlew.net/2014/10/08/grokking-rxjava-part-4/) - published in Sep/Oct 2014 by Daniel Lew
3+
See the [RxAndroid](https://github.com/ReactiveX/RxAndroid) project page and the [the RxAndroid wiki](https://github.com/ReactiveX/RxAndroid/wiki) for details.

0 commit comments

Comments
 (0)