1

I've been doing a bunch of research and looking over the documentation for ASyncTask in Android, but I just can't seem to wrap my head around it. I simply want to run some methods while a fragment is visible in an application, but in order to more easily do that, I think I should be using ASyncTask. My example code is as follows:

private class syncExample extends ASyncTask <Void, Void, Void>
{
    @Override
    protected void onPreExecute()
    {

    }
    @Override
    protected void doInBackground(Void... voids)
    {

    }
    @Override
    protected void onProgressUpdate()
    {

    }
    @Override
    protected void onPostExecute()
    {

    }
}

Now my questions are as follows:

  1. In the angle brackets, I have Void, Void, Void. What exactly do those represent and how do I know what's correct to place in there?

  2. For each method within the class, I have the each method called as void. When should they be different than void (like boolean, String, long, etc.)?

  3. For the doInBackground method, I have Void... voids in the parenthesis. What exactly should I be putting in there? What do they represent?

Thank you for your help. The documentation on this is not very clear for a beginner like myself.

Patr3xion
  • 122
  • 2
  • 15
  • 1
    The angle brackets are for generics. They are a java language feature. Note how all your callback methods return void and doInBackground takes a Void... vararg parameter? See what happens if you change one or more of those three types in the angle brackets to another type, like Integer or String. Once you've grokked that, read up on the source for AsyncTask to understand it's usage. https://developer.android.com/reference/android/os/AsyncTask.html – Karakuri Mar 16 '14 at 06:00
  • I've been reading that resource extensively, but it's still not clear to me. – Patr3xion Mar 16 '14 at 06:02
  • possible duplicate of [AsyncTask Android example](http://stackoverflow.com/questions/9671546/asynctask-android-example) – marcinj Mar 16 '14 at 19:40

4 Answers4

11

AsyncTask uses parameterized types (java generics) so that you can specify the types it uses when you define your own AsyncTask. Perhaps it's easier to explain in this form:

public abstract class AsyncTask<Params, Progress, Result> {
    ...
    protected abstract Result doInBackground(Params... params);

    protected abstract void onProgressUpdate(Progress... progress);

    protected abstract void onPostExecute(Result result);
    ...
}

There are no classes named Params, Progress, or Result. These are instead generic types. They are just placeholders for types you wish to use when you define your own AsyncTask subclass. The above could equally be written as such:

public abstract class AsyncTask<A, B, C> {
    ...
    protected abstract C doInBackground(A... params);

    protected abstract void onProgressUpdate(B... progress);

    protected abstract void onPostExecute(C result);
    ...
}

Suppose I were defining an AsyncTask that takes a list of Strings representing URLs, and it will ping each one to see if it's reachable, then return the number that were reachable. Meanwhile, with each test, it will update a ProgressBar as each test completes. It might look something like this:

public class MyAsyncTask extends AsyncTask<String, Integer, Integer> {

    @Override
    protected Integer doInBackground(String... params) {
        int total = params.length;
        int successfulPings = 0;
        for (int i = 0; i < total; i++) {
            if (isReachable(params[i])) {
                successfulPings++;
            }
            publishProgress(i, total);
        }
        return successfulPings;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        int testsSoFar = progress[0];
        int totalTests = progress[1];
        progressBar.setMax(totalTests);
        progressBar.setProgress(testSoFar);
    }

    @Override    
    protected void onPostExecute(Integer result) {
        Toast.makeTest(context, "Reached " + result + " sites.", Toast.LENGTH_SHORT).show();
    }
}

I would initiate this as follows:

String[] urls = ...
MyAsyncTask task = new MyAsyncTask();
task.execute(urls);

The argument passed into execute will be passed into doInBackground. Whatever you do in doInBackground, you need to return something that gets passed in as the argument to onPostExecute. While in doInBackground, you can call publishProgress, where you can do something like I did (but you don't have to).

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • I'm rescinding my previous comment. I'm still a little confused. Why is your parameter a String...? Why are the progress and result Integer...? How do you know what to make each of those? – Patr3xion Mar 16 '14 at 07:26
  • @Patr3xion, _you_ choose the types that are appropriate for what you need to accomplish. E.g., if you need to do something in `doInBackground()` that deals with `Bitmap`s instead of `String`s, you would choose `Bitmap...` for Params. If you want to `publishProgress()` as a decimal number instead of a whole number, you might use `Float...` for Progress. If you just want a true/false for your result, Return would be `Boolean...`. – Mike M. Mar 16 '14 at 08:04
  • Ok, that makes more sense. For some reason I'm having trouble wrapping my head around ASyncTask and it's helping that you're holding my hand through it. I appreciate the help. – Patr3xion Mar 16 '14 at 08:06
  • @Patr3xion, no problem. Please note, I got a little trigger-happy with the "..." in my comment. Return is just a singular Object; i.e., it would just be `Boolean` in my example, _not_ `Boolean...`. – Mike M. Mar 16 '14 at 08:37
0

My basic suggestion is to research more about AsyncTask but I'll as well try and Answer your questions:

1- In the angle brackets, I have Void, Void, Void. What exactly do those represent and how do I know what's correct to place in there?

They are the input argument types for the overridden methods: doInBackground() onPreExecute(), onProgressUpdate() and ... respectively.

2- For each method within the class, I have the each method called as void. When should they be different than void (like boolean, String, long, etc.)?

I'll give you an example: onPostExecute() is guaranteed to be called after doInBackground(), so if onPostExecute(int someCounter) has an integer in its input, then doInBackground() should return an integer rather than void. So it will be declared like this:

 int doInBackground(){
//bla bla bla
int someIntegerValue;
return someIntegerValue;
}

3- For the doInBackground() method, I have Void... voids in the parenthesis. What exactly should I be putting in there? What do they represent?

They are var-args, basically speaking, they represent Variable number of Arguments. Read more about them here.

Once again I suggest you delve into threading in Android and get a sense of why AsyncTask is needed. Good Luck,

Community
  • 1
  • 1
Behnam
  • 6,510
  • 6
  • 35
  • 65
0

The three types used by an asynchronous task are the following:

  1. Params, the type of the parameters sent to the task upon execution. i.e if you want to send some variable/array to your async task background task. You use that information using that variable.

  2. Progress, the type of the progress units published during the background computation. i.e to show the progress of your background progress. ( such as showing how much a video/image is downloaded)

    1. Result, the type of the result of the background computation. i.e the result that you calculated in background process used for passing result to onPostExecute method.

String[] username;

username[0]="user1"; username[1]="user2";

new asynctask().execute(username);

private class asynctask extends AsyncTask <USER, Integer, String>
{
@Override
protected void onPreExecute()
{
   // anything you want to do prior starting the async task.
}
@Override
protected String doInBackground(USER... users)
{
     int count = users.length;
     for (int i = 0; i < count; i++) 
        retriveinformation(users[i]); 
return "Hey";
}
 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(String result) {

         // the value result is HEY that is returned by doInBackground.
 }

}

Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void.

information taken from https://developer.android.com/reference/android/os/AsyncTask.html

Vaibhav Ajay Gupta
  • 463
  • 2
  • 4
  • 18
0

Let me try my hand at this. All the info I am parsing is from the link shared by Karakuri in the comments to the question.

The angle brackets' contents, in order, are called "parameters", "progress", and "result". Parameters is the type of parameters (using varargs) that will be fed to "doInBackground". They will be receive by that method in an array. You will use these parameters as your data to work on in the background thread.

"Progress" is the types of values fed into the onProgressUpdate. When you override this method, you will take in data of this type, format it, and use it in the progress display. From my reading, it is up to you to publish your progress in doInBackground to be consumed by onProgressUpdate.

"Result" is the data type of the result of the operation. This will be the return type of doInBackground, as well as the parameter type of onPostExecute. For parameters that include the "..." (called varargs), you can put in as many arguments as you want. They must be separated by commas, and must all be of the type specified before the "...". The items will be passed to the function in an array. So, to answer your questions:

  1. You'll change the first parameter in the angle brackets from void when your background task needs to receive data to work on. The type will be the type of data you'd like to pass in. You'll change the second parameter when you wish to keep track of the progress. The type will be whatever type you'd like to use for keeping track, and you will need to do all the tracking yourself in "doInBackground". You'll change the third parameter when your background task needs to return data. The type will be the type of data you wish to return. The data will be supplied to "onPostExecute". This is important because AsyncTask.execute() has no return value, so you must use any necessary data in that method to get it out to the class that called AsyncTask.execute().
  2. onPreExecute appears to always be type void. doInBackground will be of the type of result, the 3rd argument in your angle brackets. onProgressUpdate appears to always be void. onPostExecute appears to be always void.
  3. The parameter of doInBackground will be a type label followed by the three dot and a name. The type for the label must be the same as the first parameter in the angle brackets. The name is anything you want. That name will then be assigned to an array of the type named in the type label, and passed in for you to use in the body of doInBackground.

Some thoughts: if you are a beginner, leave "progress" void for now, and don't override onProgressUpdate. It is very important in production code, but a lot of extra hard work for beginner learning projects. onPreExecute is also likely more trouble than it is worth at your level. Just remember they exist, and when you need them, you will likely know. Also, good work on not avoiding the subject of multithreading just because it is difficult.

LeBeau
  • 161
  • 1
  • 7