DEV Community

Cover image for How to use Anko Library to run background task in Android
Nwokocha wisdom maduabuchi
Nwokocha wisdom maduabuchi

Posted on • Updated on

How to use Anko Library to run background task in Android

Alt Text

Today I will tell you how easy it is to use Anko Library to run background tasks in android.

There are so many libraries(Rxjava, Loader, AsynTask and more) that can help you do the job but most are very complex and make it tasky to achieve a simple goal of getting out of the main thread.

Coroutines came to the rescue and made everything pretty simple, but Anko team wanted developers to have more time for there logic than learning much on how to work with Coroutines.

Anko takes advantage of Kotlin power to offer an easy and light solution to free the main thread from long-running tasks.

Below are the steps to run a background task with Anko Library

Add the dependency to your project

The first thing is to include the dependency.

As Anko does many things, the size of the library got out of hand. So there came a time when they decided to break it.

For this example, you only need to add the following import:

implementation 'org.jetbrains.anko:anko-common:0.9'

Run the task in a background thread

doAsync {
    var result = runLongTask()
    uiThread {
        toast(result)
    }
}

The code above will run your background task easily.

Top comments (0)