AsyncTasks in Android | Tutorial

Asynchronous calls are made when you need to do some heavy computing or you need to wait for something to happen, for example you’re waiting for an image to be done downloading, and you don’t want to interrupt your main UI thread. If you do not use asynchronous calls when you’re downloading a image that you’re going to display in your app from the internet, your entire UI is going to freeze until the download is complete. That’s why Google recommends that you use some sort of multithreading method when you’re doing anything that could take a little bit longer. The go-to multithreading method for Android developers is AsyncTask and that’s what we’re going to cover in this post.

What is multithreading programming?

Every app that you create is by default run on the main thread. There are two types of multithreading – there is the one that the OS is using (that’s the type that allows multiple apps to run at the same time) and we can not control that one, and there is the one that your app uses. The latter one allows your app to perform multiple actions at the same time. If you’ve ever looked at a CPU specs sheet you would have seen something like 8 cores and 18 threads. Those 8 cores are 8 physical processing units of your CPU, and the 16 threads are produced via firmware and allow each of the processing units to run two threads at the same time. But your computer is not constrained only to running 16 processes at the same time if your CPU has 16 threads. You can run multithreaded apps on a single threaded CPU, but then each process gets a certain amount of processing time, and which task will get more time depends on the priority of the task, which is set by the developers.

AsyncTask example

As you can see, three sets parameters are passed to the AsyncTask class. Those are:

  • Params – this is the type of parameters passed to the task upon execution
  • Progress – the type of the progress units published during the background computation
  • Result – the type of the result that the computation is going to return

And you execute the task in your main thread like this:

Threading rules

When to use AsyncTask

Sometimes people use AsyncTasks for things that aren’t really that computing heavy. You don’t need to query your SQLite database in a separate thread if you only have 30-100 rows. If you have more than 100 rows, then you should store your database on some server and keep only the most vital data. Firebase is really good for those kind of things, unless you’re making something like a spaced repetition app in which case you are going to need to keep a large database in your memory at all times. Use AsyncTask almost always when you’re downloading some data from the internet. You don’t want your UI to freeze until some little piece of data is downloaded. You could encounter bad internet signal and your app is going to just stand there completely unusable, but if you do this in a separate thread you could display some kind of message that the internet signal is weak and you will not be able to display the data wanted data. In short – use separate threads when dealing with the internet.