Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend from AsyncTask as a generic base class?

There are three different implementations of an AsyncTask:

public class DownloadTask extends AsyncTask<String, Integer, Boolean>
public class JsonParserTask extends AsyncTask<Object, Void, Boolean>
public class PostCommentTask extends AsyncTask<String, Void, HttpRequestResult>

I would like them to extend a BaseAsyncTask which I can use for dependency injection then. The class signatur of AsyncTask looks like this:

public abstract class AsyncTask<Params, Progress, Result>

How can I extend AsyncTask while maintaining the different parameters?

                               | DownloadTask
AsyncTask <-- BaseAsyncTask <--| JsonParserTask
                               | PostCommentTask
like image 893
JJD Avatar asked Sep 19 '13 16:09

JJD


People also ask

Why AsyncTask is deprecated?

Why Android AsyncTask is Deprecated? Here is the official reason it is deprecated. AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.

What is the use of AsyncTask class?

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

How do I stop AsyncTask when activity is destroyed?

You can either cancel the AsyncTask in the onStop method of your activity or you can let your async task finish, and not loose its progress and relink it to the next instance of your activity.


1 Answers

Try:

abstract class BaseAsyncTask<Params, Progress, Result> 
                  extends AsyncTask<Params, Progress, Result>
like image 152
CommonsWare Avatar answered Nov 03 '22 12:11

CommonsWare