The RxRecipes series generally refers to Android and RxJava2, but the same recipes can apply to any Rx implementation and platform (RxJs, RxSwift, a backend with RxJava, etc). Previous RxRecipe: Wrap your way to Rx

Challenge: Reactive source from Lifecycle Events

In my previous article, I talked about a simple way to convert an imperative API to a reactive one (with fromCallable). There are many times however, where it isn’t that simple.

Imagine you need some component to react to Activity lifecycle events.

Cleaning up a cache when your Activity stops.

Recording a specific analytics event every time your Activity pauses.

Binding some Android specific component (like a TextWatcher) to the Activity’s start.

However, you immediately run into the problem of where to put your Rx code. There is no simple method or entry point.

See The Official Android Activity Lifecycle Documentation

These hooks happen at different points, and can happen multiple times in different orders and sections depending on user input, application logic, and operating system events (like a facebook message pop-up triggering onPause()).

We want to get to something like this:

All the activity lifecycle events in one simple stream. Sweet!

But how do we get there?