Integrating RxJava with Activity Lifecycle

There are so many important things to know before one can start building robust Android applications with RxJava that it's hard to pick which things to cover first. However, Android Activity Lifecycle management along with RxJava subscriptions will probably be more important (and more difficult).

Without proper awareness and integration with the lifecycle, it is quite easy to lose control of Subscriptions and Observables. We will see that it often leads to memory leaks and tasks that never terminate. However, we will also cover ways to prevent that from happening.

First of all, we will review the Android Activity Lifecycle, and we will pay extra attention to the places where RxJava is used most often.

Next, multiple ways will be explored to prevent loose Subscriptions. We will introduce the reader to ComposedDisposable to cancel Subscriptions at appropriate times in the Android Activity Lifecycle. Further, we will learn how to use RxLifecycle (https://github.com/trello/RxLifecycle) to make the management of Subscriptions a super easy task.

The topics covered in this chapter are as follows:

  • A short refresher on Activity and Fragment Lifecycles
  • What resource leaks are and how they usually happen
  • How can leaks be avoided
  • How to set up and use the RxLifecycle library to avoid leaks
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.158.134