Basic basis of Android asynchronous processing "AsyncTask"

About Android asynchronous processing "AsyncTask"

I was using AsyncTask somehow, so I tried to summarize the basics again.

What is asynchronous processing in the first place?

Synchronous processing: To execute certain tasks in order. Asynchronous processing: When one task is executing, another task executes another processing.

Why you need to process asynchronously

Let's consider the case where asynchronous processing is not performed. When the user is operating on Android, Swift, web application, etc., for example, when acquiring some information (video list, search result list, etc.), if it is synchronous processing, other than UI etc. until the acquisition process is completed Cannot be processed at all and the screen remains stationary. If that happens, the user will feel that the app has stopped (bug).

If implemented by asynchronous processing, it is possible to perform information acquisition (video list, search result list, etc.) processing in parallel while making the UI / screen look good. By doing so, you can show the user that you are getting information now and avoid misunderstandings.

If you look closely, you can often see a loading screen that moves on the screen while loading Youtube and SNS.

What is AsyncTask?

You can use Handler and ThreadHandler classes for Android asynchronous processing, and AsyncTask is one of these Helper Classes.

AsyncTask is easier to handle async than any other method.

Main methods of AsyncTask

onPreExecute() It is executed in the main thread before executing the doInBackground method. Used when you want to perform some processing before asynchronous processing.

doInBackground() It runs in a thread separate from the main thread. Describe the content you want to process asynchronously.

onProgressUpdate() It runs on the main thread. Returns the progress to the main thread. It can be used when you want to display the progress of asynchronous processing with a progress bar.

onPostExecute() It is executed in the main thread after executing the doInBackground method. Returns the result to the main thread. You can receive the return value of the doInBackground method as an argument of this method and reflect the result on the screen.

Use scene

STEP1: Create a class that inherits AsyncTask


class asyncTask extends android.os.AsyncTask{
    @Override
    protected Object doInBackground(Object... obj){
    
   }

STEP2: Enter the contents of each method (doInBackground is required)


class asyncTask extends android.os.AsyncTask{
    //* This argument is Object...However, as usual, you can also specify the type and number.
    @Override
    protected Object doInBackground(Object... obj){
      //Fill in the process here
      System.out.plintln("The contents of asynchronous processing are described here.")
      //You can also get the arguments
      String message = (String)obj[0];
      
   }

STEP3: In the main thread, instantiate the class created in STEP1 and 2 and call it with the execute method.


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Task generation
        mAsyncTask = new asyncTask(textView);
        //Task execution
        mAsyncTask.execute("Arguments you want to pass to asyncTask here");
    }
}

Precautions when using

If you process the main thread directly with doInBackground, an exception will occur.

Summary

・ AsyncTask can be used for asynchronous processing ・ AsyncTask can use asynchronous processing more easily than other methods. ・ DoInBackground is required when using AsyncTask

Recommended Posts

Basic basis of Android asynchronous processing "AsyncTask"
The basic basis of Swift dialogs
The basic basis of Swift's Delegate
Implementation of asynchronous processing in Tomcat
Basic processing flow of java Stream
Android --Is the order of serial processing and parallel processing of AsyncTask guaranteed? ??
Asynchronous processing by RxJava (RxAndroid) on Android
Implementation of multi-tenant asynchronous processing in Tomcat
About UI thread processing in Android asynchronous
The basic basis of Swift custom cells
Implementation of asynchronous processing for single tenant in Tomcat
Image processing: The basic structure of the image read by the program
Asynchronous processing and Web API integration in Android Studio
[Swift] Asynchronous processing "GCD"
Basic format of Dockefile
Definition of Android constants
Aiming for a basic understanding of the flow of recursive processing
Programming from 51 years old Note Android Asynchronous communication callback Processing after AsyncTask ends Further generalization / standardization