[Android Software/Hacking General [Developers Only]] dk_zero-cool:
Android Async TaskManager

AsyncTask is a great tool, allowing one to run background tasks while interacting with the UI before, while and after. However it does come with a few issues, mostly related to the way Android act when a user flips the screen or in other ways creates a detachment of the UI. AsyncTask is not equipped to handle these circumstances.

TaskManager is an extension of AsyncTask which fixes it's issues (Based on an idea of Santiago Lezica) and on top of that, provides additional features.

Source & Documentation

https://github.com/SpazeDog/taskmanager

Features

Makes sure that UI inteaction waits until the UI is pressent

Makes sure that flipping the screen does not produce multiple instances running the same tasks

Adds a new inteaction to AsyncTask

onUIReady()

and

onUIPause()

which is invoked whenever the UI is detached and re-attached

Compatible with both native fragments and the support library

Helper methods that makes it easy to use it in both Activities and Fragments

Deviates

Like mentioned above, this is based on the idea of Santiago Lezica whom made a pretty decent example. There was however a few things that bothered me, which made me decide to build my own from scratch.

The tasks should only be handled by the Task class and not be divided between the Task and the Manager. The Managers job should only be to keep track of the UI status and then report to the Tasks whenever this changes. It is then up to the Task what action should be taken depending on the current state.

I was missing the

onUIReady()

and

onUIPause()

methods which helps deal with attachment and detachment of the UI from within each individual task. Usefull to handle things like Progress Dialogs.

Using

onActivityCreated()

and

onDetach()

in the Manager does not work properly. They execute upon screen flip, but not on application pause and resume, which is also an attachment and detachment of the UI.

It is fine to include the AsyncTask

onProgressUpdate()

method, but without including

publishProgress()

, you can't really use it for anything.

The onPreExecute() method should be managed via the pending method like the rest of the methods, otherwise you will end up with a lot of application crashes.

If one should decide to built an application for newer Android versions only, there is no reason to use the support library, so there should be support for both.

I also wanted something that could return the current object.

getActivity()

is fine when working in an Activity, but this should also be able to run from within fragments. So a

getObject()

was added.

How it works

Take a look at Santiago's blog

public class MyActivity extends Activity {

private ProgressDialog mProgressDialog;

@[URL="http://xdaforums.com/member.php?u=439709"]override[/URL]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

new Task<Context, Void, Boolean>(this, "nameOfMyAsyncLoader") {
@[URL="http://xdaforums.com/member.php?u=439709"]override[/URL]
protected void onUIReady() {
((MyActivity) getObject()).mProgressDialog = ProgressDialog.show( (Activity) getActivityObject(), "", "Loading...");
}

@[URL="http://xdaforums.com/member.php?u=439709"]override[/URL]
protected void doInBackground(Context... params) {
return someMethod( params[0] );
}

@[URL="http://xdaforums.com/member.php?u=439709"]override[/URL]
protected void onPostExecute(Boolean result) {
((MyActivity) getObject()).mProgressDialog.dismiss();

if ( result ) {
...

} else {
...
}
}

}.execute( getApplicationContext() );
}
}

No mater if you execute this from within an Activity, Fragment or the same using the Support Library, you just parse

this

to the constructor. If you execute it from within a fragment, you need to make sure that you parsed a Tag Name when you added it with FragmentManager. TaskManager does not save instances of the object you parse, it just saves the information needed for it to find it again. With fragments, this means Tag Names. Without it,

getObject()

will return

NULL

on Fragments.

Download Pre-built Jar

2.0.2 (2013-06-28) (MD5: 88dbced8ebc4db9dea1b9cf12ab0da40)

[Default Mirror] SourceForge.net