Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use AsyncTask correctly in Android [closed]

I don't want to pass any arguments to doInBackground method of the AsyncTask.

So what should be the code like?

like image 926
omerjerk Avatar asked Jan 10 '13 04:01

omerjerk


People also ask

Can we use AsyncTask in Android?

Android AsyncTask going to do background operation on background thread and update on main thread. In android we cant directly touch background thread to main thread in android development. asynctask help us to make communication between background thread to main thread.

What can I use instead of AsyncTask in Android?

Alternative 1: Using Executor and Handler The executor will help in performing any task in the background and the handler will help to make UI changes.

What is the problem with AsyncTask in Android?

In summary, the three most common issues with AsyncTask are: Memory leaks. Cancellation of background work. Computational cost.

What happens to AsyncTask if activity is destroyed?

If you start an AsyncTask inside an Activity and you rotate the device, the Activity will be destroyed and a new instance will be created. But the AsyncTask will not die. It will go on living until it completes. And when it completes, the AsyncTask won't update the UI of the new Activity.


2 Answers

import android.app.Activity; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle;  public class AsyncExample extends Activity{   private String url="http://www.google.co.in";  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState); }   @Override protected void onResume() {     // TODO Auto-generated method stub     super.onResume();      new AsyncCaller().execute();  }  private class AsyncCaller extends AsyncTask<Void, Void, Void> {     ProgressDialog pdLoading = new ProgressDialog(AsyncExample.this);      @Override     protected void onPreExecute() {         super.onPreExecute();          //this method will be running on UI thread         pdLoading.setMessage("\tLoading...");         pdLoading.show();     }     @Override     protected Void doInBackground(Void... params) {          //this method will be running on background thread so don't update UI frome here         //do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here           return null;     }      @Override     protected void onPostExecute(Void result) {         super.onPostExecute(result);          //this method will be running on UI thread          pdLoading.dismiss();     }      } } 
like image 115
Mehul Joisar Avatar answered Oct 14 '22 06:10

Mehul Joisar


According to AsyncTask, its

 AsyncTask<Params, Progress, Result> 
  • Params, the type of the parameters sent to the task upon execution.
  • Progress, the type of the progress units published during the background computation.
  • Result, the type of the result of the background computation.

So if you want to pass void in doInBackground just pass void in place of Params.

Example code:

class DownloadLink extends AsyncTask<Void, Void, Void> {           @Override         protected Void doInBackground(Void... params) {             // TODO Auto-generated method stub              //Do Your stuff here..             return null;         }     } 

And call it as:

 new DownloadLink().execute(); 
like image 30
Name is Nilay Avatar answered Oct 14 '22 08:10

Name is Nilay