Sunday , November 28 2021

Android AsyncTask is Deprecated: Here’s another way – Android Coding by DMTechnolab

If you’re in Android development, I’m pretty sure you know about it Android AsyncTask. The AsyncTask class helped us execute some code in the background thread.

With the help of AsyncTask we can execute something on the background thread and return the result to the UI thread. If you want to learn about AsyncTask you can see this Android MySQL tutorial, Where I used AsyncTask.

But Android AsyncTask is depicted at API level 30.

So what are the options now? That is what I will tell you in this post.

Why has Android AsyncTask been changed?

Here is the official reason for this.

The purpose of the AsyncTask was to enable the proper and easy use of UI threads. However, the most common use case was for integrating into the UI, and this would cause a context leak, missed callback, or crash on a configuration change. It also behaves inconsistently on different versions of the platform, swallowing exceptions from doInBackground, and does not provide much utility when directly using executables.

AsyncTask option

Officially recommended option Kotlin Koratines, That you should use writing Asynchronous code In your project

You can see it full Kotlin Coroutes Tutorial I have already published.

But if you are a beginner and you have just started learning Android development then jump straight Kotlin Cortine Is not recommended. So in this post I will show you something that does not require any dependency.

Using executables

we have Java.Useful.Concurrent.Expenditure expenditure expenditure expenditure class; If you do not want to use then we can use in place of AsyncTask Kotlin Koratines.

Here is an example of how you can use it.

It’s the same thing that you are using AsyncTask .

But still it is recommended that you use Kotlin Koratines To write asynchronous code in your project.

So this is for the Android AsyncTask option. If you have some input on your behalf, please comment below. And yes don’t hesitate to ask if you have any confusion or problem. Thank you

Belal Khan ProfileBelal Khan Profile

Hi, my name is Belal Khan and I am Google Developers Specialist (GDE) for Android. A passion for teaching made me this blog. If you are an Android developer, or you are learning about Android development, I can help you a lot with simplified coding.


Source link

About dmtechnolab

Check Also

Simplified Coding

Android Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android …

Leave a Reply

Your email address will not be published. Required fields are marked *