Android and RxJava: Using a Single

In the last article, we took a basic look at RxJava, and used Observable.create() to create our Observable. We also setup an Observer that listens for data. In this blog post, we’ll talk about Single.just() and how it differs from Observable.just(). Let’s get started!

Let’s make some changes to our MainActivity:

What exactly is going on here? First, we changed firstNames to firstName. As you can tell, we only pass in one name, “Daniel.” But the important thing to note is that Observable.just() got changed to Single.just(). What’s the difference? The difference lies with the subscriber. If you look at the observer variable, you’ll noticed a few things changed. First of all, we only have 2 callback methods: onSuccess and onError.

Before we had methods like onNext(). With Single, we only get these 2 methods: onSuccess and onError. That’s it. If it’s successful, the name “Daniel” will go to onSuccess(). If for some reason there is an error, onError() will be called.

With Single, we simply care about the 1 result, or the 1 error, that’s all. Single cannot accept multiple names like Mel, Daniel or Nathan. Just one name, like Daniel.

However, with Single, you can pass a list of names, but the names will be in List<String> instead of just String.

Daniel Malone Daniel Malone
Bringing more than a decade of software engineering experience, Daniel Malone is Editor at androidEveryday. An Austin native, Daniel is often found reading technical books, blogging and creating YouTube tutorials. When not working, he likes to listen to pop hits on Google Play Music.

Android RecyclerView Tutorial (Part 1)

  • Most apps contain lists of data using RecyclerView.
  • In this tutorial, use ConstraintLayout and Kotlin to display a list of data.

3 years ago

findViewById() in Kotlin

  • As you may have discovered, findViewByid() is no longer needed.
  • Kotlin adds the ability to directly access Views.

3 years ago

Android Architecture Components Tutorial: ViewModel + LiveData

  • Use Kotlin to build a basic Android app using Android Architecture Components.
  • As part of Jetpack, LiveData and ViewModel support a MVVM app architecture.

3 years ago

Android SharedPreferences Tutorial in Kotlin

  • Use SharedPreferences to store simple data in Android Studio projects.
  • It's good for storing small amounts of data

    3 years ago

Cleaner Architecture for Android Apps

  • Clean Architecture for Android can help developers prepare for change.
  • But app developers can't possibly know what will change.

    3 years ago

Android RecyclerView with Kotlin – Part 3

  • In part three of this series, finish creating the RecyclerView.
  • Use onBindViewHolder to display data.

3 years ago

Android RecyclerView with Kotlin – Part 2

  • Create the entire RecylerView for our UsersAdapter.
  • Continued from part 1, where we set up the Android Studio project.

3 years ago

Android and RxJava: Getting Started

  • The powerful library RxJava can help developers build better apps.
  • See how to use Observables and Observers in this tutorial.

3 years ago

Android RecyclerView with Kotlin - Part 1

  • Oftentimes, developers need to display a list of some sort of data.
  • Whatever type of data is displayed, you should use a RecyclerView.

3 years ago

Display Sample Data when Creating a RecyclerView

  • Android Studio's default RecyclerView preview lacks sample data.
  • Adding sample data can be helpful for debugging.

3 years ago

Android Studio Tutorial with Kotlin (2018 Edition) - Part 2

  • Create a UI in Android Studio using the Design mode.
  • In this latest installment for beginners, we'll add a widget to our screen.

3 years ago