Ben Taliadoros
Ben Taliadoros

Reputation: 9381

Passing nothing to an AsyncTask in Android

How do I instantiate and call and class extending AsyncTask if I am passing nothing to it?

Also, I am setting a textview in the UI to the calculated result.

Thanks, Ben

Upvotes: 15

Views: 16062

Answers (2)

d.danailov
d.danailov

Reputation: 9800

Example implementation for Async without params and result Bitmap in onPostExecute result

/**
 * My Async Implementation without doInBackground params
 * 
 */
private class MyAsyncTask extends AsyncTask<Void, Void, Bitmap> {

    @Override
    protected Bitmap doInBackground(Void... params) {

        Bitmap bitmap;

        .... 

        return bitmap;
    }

    protected void onPostExecute(Bitmap bitmap) {

        ....
    }
}

In your activity, you should to add this implementation:

MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();

Upvotes: 32

Lars
Lars

Reputation: 4082

I think what you meant to ask was how do I write an AsyncTask that doesn't ask for any parameters. The trick is to define what you expect to use as parameter and return value in the extension of your class: class MyClass extends AsyncTask<Void, Void, Void> for example doesn't expect any parameters and doesn't return any either. AsyncTask<String, Void, Drawable> expects a String (or multiple strings) and returns a Drawable (from its own doInBackground method to its own onPostExecute method)

Upvotes: 11

Related Questions