Image Source

Getting Started 🎬

We will add transitions to an app that has two fragments. DogListFragment contains a recycler List while the DogDetailsFragment contains just an Imageview and a textview.

You might come across a situation while developing an app where you’d want to retry a certain request when an Exception occurs without user’s intervention.

A pragmatic example would be a case of an IO Exception. Consider this use case. You want to fetch a list of dogs from the server, however, you get an IO exception due to a network issue.

Now you can either trigger another request right away or wait for sometime (backoff) and retry. In this article we will explore the backoff method.

When you want to trigger a network request whenever the input text is edited, you will end up shooting a lot of requests for each letter added or removed.(eg Google search)

To solve this issue, you need to reduce the number of requests by waiting for some time until the user has finished typing a whole word. For Example, when the user is typing “Et”… “Eter”….. “Etern”….. “Eternal”. You intelligently only want to trigger a request when user has typed the whole word “Eternal”.

In this article, we will use the latest StateFlows API to create a validation form.

Adding animations to your android app makes it more fun and interesting as compared to static text or pictures. The easiest way to do that is using a 3rd party library Lottie by airbnb.

The best part is you can use animations without having to write a single line of code.

For brevity, we will use ready made animations available on Alternatively you can also build your own custom animations using Adobe after effects and use them in your app. (See it in action here)

Let’s begin 🏁

Let’s start by downloading all the animation files you need for your project from…

Knowledge of Retrofit, Room, and Coroutines is a must for this guide.

Part 1 of this Guide Here. (Not mandatory if you know how flows works)

Let’s Begin 🏁

In this guide, we will add a search bar to the android application, which will query all the dog breeds in the database and update the flow based on out query using channels(Github repository here).

Knowledge of Retrofit, Room and Coroutines is a must for this guide.

In this tutorial We will build an app that fetches dog breeds from DogApi using Retrofit and then save it to database using Room. We will then use Flows to display the list of dogs from the database.

Let’s get Started! 🏁

First things first.

What are flows and when do you use one?

A flow is cold asynchronous data stream that sequentially emits values and completes normally or with an exception.

This basically means that you should use flows when there’s a stream of data to be emitted. …

Prerequisite — You need to have a good understanding of coroutines ,MVVM architecture, Retrofit for this guide.

Part 1 : Learn By Doing: Coroutines — A Guide for using coroutines in Android for Retrofit Requests

In part 1, I used viewModelScope.launch{} for executing a coroutine. In this guide I’ll use live data builder instead.

In the the viewModel, I call loadTopTwoDogsAsync() in init{} where mainActivityRepository.getTopTwoDogsAsync() is a suspend function which is an api request that runs on background thread.

And then get() and observe the topDogsAsync live data from the View.

Now you’re probably wondering where does Live…

Prerequisites —Basic knowledge of Coroutines. You can use this guide for help.

The Synchronous Approach

I use val dogBreedList = api.getBreedsList().message.keys.toList() to fetch all the breeds and convert it into a List.

This happens in background thread ,Dispatcher.IO as explained in Part 1 because of the suspend modifier in retrofit requests in MainActivityApi.kt.

After fetching the breeds, for each breed api.getImageByUrl(it).message is called and then waited on until a result in returned after which the same api is called for another breed.

This happens for about 50 breeds Synchronously, and takes over 30 Seconds.

The Asynchronous Approach

Instead of loading images one by one…

In this guide, we will build an app that fetches two random breeds and a list of dog breeds and images and display it via a recycler using coroutines.

Let’s Dive in! 🤿

Coroutine Scope

You need a Coroutine Scope to launch a Coroutine. This basically ties a Coroutine to the lifecycle from where it is launched. When the lifecycle is killed, all the Coroutines in that Scope will be terminated hence helping in handling memory leaks.

You’ll mostly need to use lifecycleScope, for when you launch a coroutine from an Activity/Fragment or ViewModalScope , from when you launch from a viewModel and GlobalScope

Shivam Dhuria

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