com.github.droidfu.concurrent
Class BetterAsyncTask<ParameterT,ProgressT,ReturnT>

java.lang.Object
  extended by android.os.AsyncTask<ParameterT,ProgressT,ReturnT>
      extended by com.github.droidfu.concurrent.BetterAsyncTask<ParameterT,ProgressT,ReturnT>
Type Parameters:
ParameterT -
ProgressT -
ReturnT -

public abstract class BetterAsyncTask<ParameterT,ProgressT,ReturnT>
extends android.os.AsyncTask<ParameterT,ProgressT,ReturnT>

Works in a similar way to AsyncTask but provides extra functionality. 1) It keeps track of the active instance of each Context, ensuring that the correct instance is reported to. This is very useful if your Activity is forced into the background, or the user rotates his device. 2) A progress dialog is automatically shown. See useCustomDialog() disableDialog() 3) If an Exception is thrown from inside doInBackground, this is now handled by the handleError method. 4) You should now longer override onPreExecute(), doInBackground() and onPostExecute(), instead you should use before(), doCheckedInBackground() and after() respectively. These features require that the Application extends DroidFuApplication.


Nested Class Summary
 
Nested classes/interfaces inherited from class android.os.AsyncTask
android.os.AsyncTask.Status
 
Constructor Summary
BetterAsyncTask(android.content.Context context)
          Creates a new BetterAsyncTask who displays a progress dialog on the specified Context.
 
Method Summary
protected abstract  void after(android.content.Context context, ReturnT result)
          A replacement for onPostExecute.
protected  void before(android.content.Context context)
          Override to run code in the UI thread before this Task is run.
 void disableDialog()
          Disable the display of a dialog during the execution of this task.
protected  ReturnT doCheckedInBackground(android.content.Context context, ParameterT... params)
          Override to perform computation in a background thread
protected  ReturnT doInBackground(ParameterT... params)
           
 boolean failed()
          Has an exception been thrown inside doCheckedInBackground()
protected  android.content.Context getCallingContext()
          Gets the most recent instance of this Context.
protected abstract  void handleError(android.content.Context context, Exception error)
          Runs in the UI thread if there was an exception throw from doCheckedInBackground
protected  void onPostExecute(ReturnT result)
           
protected  void onPreExecute()
           
 void setCallable(BetterAsyncTaskCallable<ParameterT,ProgressT,ReturnT> callable)
          Use a BetterAsyncTaskCallable instead of overriding doCheckedInBackground()
 void useCustomDialog(int dialogId)
          Use a custom resource ID for the progress dialog
 
Methods inherited from class android.os.AsyncTask
cancel, execute, get, get, getStatus, isCancelled, onCancelled, onProgressUpdate, publishProgress
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BetterAsyncTask

public BetterAsyncTask(android.content.Context context)
Creates a new BetterAsyncTask who displays a progress dialog on the specified Context.

Parameters:
context -
Method Detail

getCallingContext

protected android.content.Context getCallingContext()
Gets the most recent instance of this Context. This may not be the Context used to construct this BetterAsyncTask as that Context might have been destroyed when a incoming call was received, or the user rotated the screen.

Returns:
The current Context, or null if the current Context has ended, and a new one has not spawned.

onPreExecute

protected final void onPreExecute()
Overrides:
onPreExecute in class android.os.AsyncTask<ParameterT,ProgressT,ReturnT>

before

protected void before(android.content.Context context)
Override to run code in the UI thread before this Task is run.

Parameters:
context -

doInBackground

protected final ReturnT doInBackground(ParameterT... params)
Specified by:
doInBackground in class android.os.AsyncTask<ParameterT,ProgressT,ReturnT>

doCheckedInBackground

protected ReturnT doCheckedInBackground(android.content.Context context,
                                        ParameterT... params)
                                 throws Exception
Override to perform computation in a background thread

Parameters:
context -
params -
Returns:
Throws:
Exception

handleError

protected abstract void handleError(android.content.Context context,
                                    Exception error)
Runs in the UI thread if there was an exception throw from doCheckedInBackground

Parameters:
context - The most recent instance of the Context that executed this BetterAsyncTask
error - The thrown exception.

onPostExecute

protected final void onPostExecute(ReturnT result)
Overrides:
onPostExecute in class android.os.AsyncTask<ParameterT,ProgressT,ReturnT>

after

protected abstract void after(android.content.Context context,
                              ReturnT result)
A replacement for onPostExecute. Runs in the UI thread after doCheckedInBackground returns.

Parameters:
context - The most recent instance of the Context that executed this BetterAsyncTask
result - The result returned from doCheckedInBackground

failed

public boolean failed()
Has an exception been thrown inside doCheckedInBackground()

Returns:

setCallable

public void setCallable(BetterAsyncTaskCallable<ParameterT,ProgressT,ReturnT> callable)
Use a BetterAsyncTaskCallable instead of overriding doCheckedInBackground()

Parameters:
callable -

useCustomDialog

public void useCustomDialog(int dialogId)
Use a custom resource ID for the progress dialog

Parameters:
dialogId -

disableDialog

public void disableDialog()
Disable the display of a dialog during the execution of this task.



Copyright © 2011. All Rights Reserved.