Kotlin Coroutines and Flow for Android Development 2023

The Complete Guide! Get a deep understanding of Kotlin Coroutines and Flow to use them successfully in your Android Apps
Kotlin Coroutines and Flow for Android Development 2023
File Size :
6.17 GB
Total length :
15h 21m



Lukas Lechner


Last update

Last updated 11/2022



Kotlin Coroutines and Flow for Android Development 2023

What you’ll learn

Implement the most common use cases for Kotlin Coroutines and Flow on Android
Get a deep understanding of Kotlin Coroutines and Flows
Learn advanced Coroutine Concepts (Structured Concurrency, Coroutine Scopes and Contexts, internal workings, )
Learn how to create Flows using different Flow builders
Learn about Flow operators (lifecycle operators, intermediate operators, terminal operators)
Learn about different kind of Flows (regular Flows, SharedFlows, StateFlows)
Create a stock live-tracking app with Flows
Learn when to use Channels in Android Development
Understand the advantages of using Coroutines over Callbacks or RxJava.
How to perform network requests with Retrofit sequentially and concurrently.
How to use Room together with Coroutines.
Background processing with Coroutines.
Exception Handling and Cancellation in Coroutines and Flows.
How to write Unit Tests for coroutine-based implementations

Kotlin Coroutines and Flow for Android Development 2023


No prior knowledge about Kotlin Coroutines or Flow needed, however you should be familiar with the Basics of Kotlin and Android, as well as some Android Jetpack Components (ViewModels, LiveData, Room) and Retrofit.


Google recommends Kotlin Coroutines and Flow as the preferred solution for asynchronous programming on Android. Sooner or later, probably every  Android developer will get in touch with these topics. This course will give you a deep understanding of Kotlin Coroutines and Flow and show how to implement the most common use cases for Android applications.This course consists of two big parts: The Coroutines part and the Flow part. Before being able to use Flows in our applications, we first need a solid understanding of Coroutines. That’s why Coroutines are covered first. However, if you already have some experience with Coroutines, then you can also start with the Flow part right away, and jump back to lessons of the Coroutines part whenever needed. In the part about Coroutines, first, we will take a detailed look at the advantages of Kotlin Coroutines over other approaches for asynchronous programming, like RxJava and Callbacks.Then, we will talk about some theoretical fundamentals. These include:Routines vs. CoroutinesSuspend FunctionsCoroutines vs. ThreadsBlocking vs. SuspendingMultithreaded CoroutinesInternal workings Next, we will implement some of the most common use cases for Kotlin Coroutines in Android applications. These include: Performing network requests with Retrofit sequentially and concurrentlyImplementing Timeouts and RetriesUsing Room with CoroutinesPerforming background processing with CoroutinesContinuing Coroutine execution even when the user leaves the screen. To improve your learning experience, this course also challenges you with several exercises. Learning Coroutines can be overwhelming because you need to learn a lot of new concepts. However, we are going to start simple and as our use cases will get more and more complex, we will learn about new concepts step-by-step. These new concepts are:Coroutine Builders (launch, async, runBlocking)Coroutine ContextCoroutine DispatchersStructured ConcurrencyCoroutine Scopes (viewModelScope, lifecycleScope, GlobalScope)Jobs and SupervisorJobsscoping functions (coroutineScope{} and supervisorScope{})Cooperative CancellationNon-Cancellable CodeWe will also make a deep dive into Exception Handling and discuss concepts like: exception handling with try/catchexception handling with CoroutineExceptionHandlerswhen to use try/catch and when to use a CoroutineExceptionHandlerexception handling in Coroutines started with launch and asyncexception handling specifics of scoping functions coroutineScope{} and supervisorScope{}Unit Tests are very important for every codebase. In the course’s final section, we will write unit tests for most of the coroutine-based use cases we implemented earlier. We will discuss concepts likeTestCoroutineDispatchercreating a JUnit4 Rule for testing coroutine-based coderunBlockingTest{} Coroutine Buildervirtual timeTesting sequential and concurrent executionTestCoroutineScopeIn the part about Kotlin Flow, we first cover all the basics. We will answer the question “What is a Flow?” and then we discuss the benefits and drawbacks of reactive programming. Afterward, we are going to have a look at different Flow builders and operators: basic flow buildersterminal operators terminal operator “launchIn()”terminal operator “asLiveData()”lifecycle operatorsintermediate operatorsIn our first real Flow use case, we use a Flow to create a live stock-tracking feature, that uses all the available basic flow components. In the next module, we will take a look at Exception Handling and Cancellation with Kotlin Flow. In the following module, you will learn about StateFlow and SharedFlow and the following concepts: how to make Coroutines lifecycle-aware with the “repeatOnLifecycle()” suspend functionHot Flows VS Cold FlowsConverting Flows to SharedFlows with the “shareIn()” operatorConverting Flows to StateFlows with the “stateIn()” operatorWhen to use SharedFlow and when to use StateFlowNext, you will learn about Channels, how they differ from hot flows, and when they are useful in Android Applications.By the end of this course, you will have a fundamental understanding of Kotlin Coroutines and Flows and be able to write readable and maintainable, asynchronous, and multithreaded Android Applications.


Section 1: Introduction

Lecture 1 Extended Course Preview

Lecture 2 Course Structure and Goals

Lecture 3 How to get the source code of the Sample Project

Lecture 4 Basic Setup of Sample Project

Section 2: Different Approaches for Asynchronous Programming

Lecture 5 Section Introduction

Lecture 6 What is Asynchronous Programming?

Lecture 7 Use Case Explanation

Lecture 8 Callback Implementation

Lecture 9 RxJava Implementation

Lecture 10 Coroutines Implementation

Lecture 11 Comparing Approaches

Lecture 12 Recap

Section 3: Coroutine Fundamentals

Lecture 13 Section Introduction

Lecture 14 Routines and Coroutines

Lecture 15 Suspend Functions

Lecture 16 Coroutines and Threads

Lecture 17 Blocking VS Suspending

Lecture 18 Multithreaded Coroutines

Lecture 19 Internal Workings

Lecture 20 How delay() works

Lecture 21 Section Recap

Section 4: Performing Network Requests sequentially

Lecture 22 Introduction

Lecture 23 Adding Coroutines to your project

Lecture 24 Coroutine Builders: Launch and RunBlocking

Lecture 25 Main-safety

Lecture 26 Basic error handling with try-catch

Lecture 27 Exercise 1

Lecture 28 Exercise 1: Solution

Lecture 29 Section Recap

Section 5: Performing Network Requests concurrently

Lecture 30 Section Introduction

Lecture 31 UseCase description

Lecture 32 Implementing UseCase#3 in a sequential way

Lecture 33 The async coroutine builder

Lecture 34 Implementing UseCase#3 in a concurrent way

Lecture 35 Implementing UseCase#4 in a sequential way

Lecture 36 Exercise 2: Implementing UseCase#4 in a concurrent way

Lecture 37 Exercise 2: Solution

Lecture 38 Section Recap

Section 6: Higher-Order Functions

Lecture 39 Implementing a timeout

Lecture 40 Implementing retries

Lecture 41 Extract retry logic into higher order function

Lecture 42 Add exponential backoff to retry

Lecture 43 Exercise 3: Combining retries and timeout

Lecture 44 Exercise 3: Solution

Lecture 45 Section Recap

Section 7: Using Room with Coroutines

Lecture 46 Section Introduction

Lecture 47 UseCase description

Lecture 48 UseCase Implementation

Lecture 49 Section Recap

Section 8: Background Processing with Coroutines

Lecture 50 Section Introduction

Lecture 51 UseCase explanation

Lecture 52 Implementation running on Main Thread

Lecture 53 Coroutine Context

Lecture 54 Coroutine Dispatchers

Lecture 55 Using withContext for context switching

Lecture 56 Coroutine Scope VS Coroutine Context

Lecture 57 Exercise4: Perform calculation in several Coroutines

Lecture 58 Exercise 4: Solution

Lecture 59 Performance Analysis

Lecture 60 Section Recap

Section 9: Structured Concurrency and Coroutine Scopes

Lecture 61 Section Introduction

Lecture 62 The unhappy path

Lecture 63 Structured Concurrency

Lecture 64 Coroutine Scopes

Lecture 65 Building up the Job Hierarchy

Lecture 66 Parents wait for Children

Lecture 67 Cancellation of parent and child jobs

Lecture 68 Job and SupervisorJob

Lecture 69 Unstructured Concurrency

Lecture 70 GlobalScope

Lecture 71 ViewModelScope

Lecture 72 LifecycleScope

Lecture 73 Scoping Functions coroutineScope{} and supervisorScope{}

Lecture 74 Continue Coroutine execution when the user leaves the screen

Lecture 75 Section Recap

Section 10: Coroutines Cancellation

Lecture 76 Cancelling Coroutines

Lecture 77 Cooperative Cancellation

Lecture 78 NonCancellable Code

Lecture 79 Making UseCase10 cooperative regarding cancellation

Lecture 80 Section Recap

Section 11: Coroutines Exception Handling

Lecture 81 Section Introduction

Lecture 82 Exception Handling with try-catch

Lecture 83 Coroutine Exception Handler

Lecture 84 Try-Catch VS Coroutine Exception Handler

Lecture 85 launch{} VS async{}

Lecture 86 Exception Handling specifics of coroutineScope{}

Lecture 87 Exception Handling specifics of supervisorScope{}

Lecture 88 Implementation of UseCase13 with try-catch

Lecture 89 Implementation of UseCase13 with CoroutineExceptionHandler

Lecture 90 Implementation of UseCase13 – show results even if a child coroutine fails

Lecture 91 Rethrowing CancellationExceptions

Lecture 92 Coroutines Exception Handling Cheat Sheet

Lecture 93 Conference Talk: Exception Handling in Kotlin Coroutines

Lecture 94 Section Recap

Section 12: Testing Coroutines

Lecture 95 Section Introduction

Lecture 96 Important Information regarding the new kotlinx-coroutines-test 1.6 APIs

Lecture 97 General Unit Testing Approach

Lecture 98 Using the TestCoroutineDispatcher for our first unit test

Lecture 99 Testing the unhappy path

Lecture 100 Creating a JUnit4 Rule for Testing Coroutine-based code

Lecture 101 Excercise 5: Create a Unit Test for UseCase2

Lecture 102 Solution: Exercise 5

Lecture 103 runBlockingTest{} and VirtualTime

Lecture 104 Testing sequential and concurrent execution

Lecture 105 Testing Timeouts (UseCase5) and Retries (UseCase 6)

Lecture 106 Testing implementations that use Dispatchers.Default or Dispatchers.IO

Lecture 107 TestCoroutineScope and Controlling Coroutine Execution

Lecture 108 Section Recap

Section 13: Basics of Kotlin Flow

Lecture 109 Section Introduction

Lecture 110 What is a Flow?

Lecture 111 Reactive Programming

Lecture 112 Starting our first Flow Use Case

Lecture 113 Basic Flow Builders

Lecture 114 Exposing a Flow in our DataSource

Lecture 115 Displaying the stock list on the screen

Lecture 116 Basic Terminal operators

Lecture 117 Terminal operator “launchIn()”

Lecture 118 Using “launchIn()” in UseCase1

Lecture 119 Lifecycle Operators

Lecture 120 Terminal operator “asLiveData()”

Lecture 121 Basic Intermediate Operators

Lecture 122 Exercise: Intermediate Operators

Lecture 123 Exercise Solution

Lecture 124 Section Recap

Lecture 125 Slides of the Section

Section 14: Flow Exception Handling and Cancellation

Lecture 126 Flow Exception Handling

Lecture 127 Exception Transparency

Lecture 128 The “retry()” Operator

Lecture 129 Exercise: Flow Exception Handling

Lecture 130 Exercise Solution

Lecture 131 Flow Cancellation

Lecture 132 Slides of the Section

Section 15: StateFlow and SharedFlow

Lecture 133 Exposing Flows instead of LiveData in the ViewModel

Lecture 134 Naive Approach – Exposing regular Flows

Lecture 135 Lifecycle-aware Coroutines with “repeatOnLifecycle()”

Lecture 136 Flows are cold

Lecture 137 SharedFlows are hot

Lecture 138 Converting Flows to SharedFlows with “stateIn()”

Lecture 139 Keeping the upstream Flow alive during configuration changes

Lecture 140 The “replay” parameter

Lecture 141 StateFlow

Lecture 142 Converting Flows to StateFlows with “stateIn()”

Lecture 143 SharedFlow VS StateFlow

Lecture 144 Section Recap

Lecture 145 Slides of the Section

Section 16: Channels

Lecture 146 Channels for Android Development

Lecture 147 Single UI Events

Lecture 148 Slides of the Section

Section 17: Thank you and Bonus

Lecture 149 Thank you and Bonus

Android Developers who want to get a deep understanding of Kotlin Coroutines and Flow and want to know how to implement the most common use cases on Android.

Course Information:

Udemy | English | 15h 21m | 6.17 GB
Created by: Lukas Lechner

You Can See More Courses in the Developer >> Greetings from CourseDown.com

New Courses

Scroll to Top