In an Android app, Kotlin flows are typically collected from the UI layer to display data updates on the screen. However, you want to collect these flows making sure you’re not doing more work than necessary, wasting resources (both CPU and memory) or leaking data when the view goes to the background.

In this article, you’ll learn how the LifecycleOwner.addRepeatingJob, Lifecycle.repeatOnLifecycle, and Flow.flowWithLifecycle APIs protect you from wasting resources and why they’re a good default to use for flow collection in the UI layer.

Wasting resources

It’s recommended to expose the Flow<T> API from lower layers of your app hierarchy regardless of…


Illustration by Virginia Poltrack

MAD Skills WorkManager, AndroidX, Audio, UX, Wear OS Tiles, Jetpack Compose, Coroutines, #AndroidDevChallenge, and a podcast

Welcome to Now in Android, your ongoing guide to what’s new and notable in the world of Android development.

NiA36 in Video and Podcast Form

This Now in Android is also offered in video and podcast form. It’s the same content, but with less reading required. The article version (keep reading!) is still the place to come for links to all of the content that’s covered.

Podcast

Click on the link below, or just subscribe to the podcast in your favorite client app.

MAD Skills: WorkManager


Illustration by Virginia Poltrack

MAD Skills MotionLayout, Material Components, KSP, AndroidX, Coroutines and Intents articles, App Security talk, and App Quality and Education announcements

Welcome to Now in Android, your ongoing guide to what’s new and notable in the world of Android development.

NiA34 in Video and Podcast Form

This Now in Android is also offered in video and podcast form. It’s the same content, but with less reading required. The article version (keep reading!) is still the place to come for links to all of the content that’s covered.

Video

Podcast

Click on the link below, or just subscribe to the podcast in your favorite client app.

MAD Skills: Motion Layout

The series on MotionLayout is coming to an end. In the fourth episode, Sean McQuillan explains how to build a collapsible toolbar using…


Coroutines are light-weight threads”, how many times have you read that? Does that mean anything to you? Probably not. Keep reading to learn more about how coroutines are actually executed on the Android runtime, how they relate to threads, and the concurrency issues that are inevitable when using the Java programming language threading model.

Coroutines and threads

Coroutines aim to simplify code that executes asynchronously. When talking about coroutines on the Android runtime, the block of code passed as a lambda to a coroutine builder ultimately gets executed on a specific thread. For example, this simple Fibonacci calculation:

// Coroutine that calculates the…


ViewModelComponent is a component in the Hilt’s component hierarchy that follows the lifecycle of a ViewModel and allows scoping types to it.

Before the ViewModelComponent was added to Hilt, ViewModel classes were created and injected by the ActivityRetainedComponent. Therefore, dependencies of ViewModels could only use unscoped types or types scoped to either the SingletonComponent or ActivityRetainedComponent whose instance is shared by all ViewModels.

The above might not seem like a problem if each screen of your app is an Activity, as scoping a type to ActivityRetainedComponent would mean each ViewModel class would receive a different instance of that type. …


If you’re a library author, you might want to make your Java-based or callback-based libraries easier to consume from Kotlin using coroutines and Flow. Alternatively, if you’re an API consumer, you may be willing to adapt a 3rd party API surface to coroutines to make them more Kotlin friendly.

This article covers how to simplify APIs using coroutines and Flow as well as how to build your own adapter using suspendCancellableCoroutine and callbackFlow APIs. For the most curious ones, those APIs will be dissected and you’ll see how they work under the hood.

If you prefer to watch a video…


Hilt got released in June 2020 as a way to standardize dependency injection (DI) in Android. For new projects, Hilt provides compile time correctness, runtime performance and scalability (read more about that here)! However, what are the benefits for an application that already uses Dagger? Should you be migrating your current app to Hilt? The following are some reasons whether your team should invest migrating from Dagger to Hilt.

✅ AndroidX extensions

If you already have Dagger working with ViewModels or WorkManager, you saw that wiring up your ViewModelFactory and WorkerFactory requires quite a lot of boilerplate code and Dagger knowledge. The most…


Illustration by Virginia Poltrack

Scoping an object A to another object B means that throughout the lifecycle of B, it’ll always have the same instance of A. When it comes to dependency injection (DI), an object A scoped to a container means that the container will always provide the same instance of A until the container is destroyed.

In Hilt, you can scope types to containers or components using annotations. For example, let’s say your app has a UserManager type that handles logins and logouts. You could scope this type to the ApplicationComponent (which is a container managed by the application’s lifecycle) using the…


Illustration by Virginia Poltrack

Hilt defines a standard way to do dependency injection (DI) in your application by providing containers for every Android class in your project and managing their lifecycles automatically for you. Hilt is built on top of the popular DI library Dagger so benefits from the compile time correctness, runtime performance, scalability, and Android Studio support that Dagger provides.

This cheat sheet allows you to quickly see what the different Hilt and Dagger annotations do and how to use them. For more information about DI and Hilt, check out these guides or read the documentation. If you prefer a step-by-step learning approach, check out the Using Hilt in an Android app codelab.

Download cheat sheet in PDF


Illustration by Virginia Poltrack

Inserting a component in the middle of the Hilt component hierarchy is not possible. However, you can create a custom component with a Hilt component as a parent and access its scoped types using entry points. In the article, we’ll see what all this implies and how to add a custom component in the Dagger 2 codelab code.

The Dagger 2 codelab goes through adding Dagger to a multiple-Activity application. As the user can log in and out of the app, user information is handled in a Dagger component with a lifespan longer than activity components.

Manuel Vivo

Android DevRel @ Google

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store