Asynchronous Calls in Koltin

A very common task in any android app is to hit any web service call and fetch some data from server. Google have always recommended to perform such heavy or time consuming calls in background thread, as if performed in main thread, these can block the UI and make the app irresponsive Traditionaly, developers use AsyncTask for these sorts of computations. But working with AsyncTask is not only complex and lengthy, but also very frustrating.

This is a simple example of AsyncTask below:

class MainActivity extends AppCompatActivity
    public void onCreate(Bundle savedInstanceState)
        Button btndownload = findViewById(;
        //Notice that I didn't need to call findViewById(), thanks to Synthetic Binding!
        btndownload.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
    public void downloadData()
        new TestAsync().execute();

class TestAsync extends AsyncTask<Void, Integer, String>
    String TAG = getClass().getSimpleName();

    protected void onPreExecute (){
        Log.d(TAG + " PreExceute","On pre Exceute......");

    protected String doInBackground(Void...arg0) {
        Log.d(TAG + " DoINBackGround","On doInBackground...");

        for(int i=0; i<10; i++){
            Integer in = new Integer(i);
        return "You are at PostExecute";

    protected void onProgressUpdate(Integer...a){
        Log.d(TAG + " onProgressUpdate", "You are in progress update ... " + a[0]);

    protected void onPostExecute(String result) {
        Log.d(TAG + " onPostExecute", "" + result);

So, if we are working on any data driven app, then for each web service call like on performing login, on getting social feed, on clicking on like button, on adding any new post, etc. we have to write all this code, which will not only make our code unreadable and complex but also very difficult to manage and test.

Kotlin comes with a solution for this using its DSL features. First you have to do is add Anko library to your gradle.

implementation "org.jetbrains.anko:anko-commons:LATEST_VERSION"

Now, we can perform this operation very easily like this:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        //Notice that I didn't need to call findViewById(), thanks to Synthetic Binding!
        btndownload.setOnClickListener { downloadData() }

    fun downloadData() {
        doAsync {
            //Execute all the long running tasks here
            val s: String = makeNetworkCall()
            uiThread {
                //Update the UI thread here
                alert("Downloaded data is $s", "Hi I'm an alert") {
                    yesButton { toast("Yay !") }
                    noButton { toast(":( !") }

That’s it!

No need to create a custom class and override all those gnarly methods just to run a long running task and update the UI from there.

Note that doAsync also aware of the underlying Activity/Fragment’s lifecyle and hence if the original Activity/Fragment which started this block is no longer alive (recreated because of configuration changes), the code inside uiThread block isn’t executed!

This greately reduces the Memory Leaks that happen in case of AsyncTasks without us having to worry about it!

If you liked this article, you can read my new articles below:

Wajahat Karim is Pakistan’s first Google Developer Expert in Android. As an experience Android developer, he deeply cares about it and keeps writing and speaking about it. He has written two worldwide 300+ pages books on Android development with more than 100 articles around the internet either on his website or his Medium publications. He is also a passionate contributor in open source and has created many Android libraries used by thousands of developers in their apps worldwide. As active public speaker, he spends lots of time giving talks in conferences and motivating people on Android development. You can find Wajahat most active on Twitter @WajahatKarim where he regularly shares all the good stuff about Android and community building.

Author's picture

Wajahat Karim

🔥 Google Dev Expert (GDE) in Android .
📱 Android Dev. 💻 Open Source Contributor . 📝 Technical Writer . 🎤 Public Speaker

Senior Android Developer

Karachi, Pakistan