Skip to content

Instantly share code, notes, and snippets.

@muyiou
Created January 10, 2014 07:35
Show Gist options
  • Select an option

  • Save muyiou/8348232 to your computer and use it in GitHub Desktop.

Select an option

Save muyiou/8348232 to your computer and use it in GitHub Desktop.
abstarct public class SimpleBackgroundTask<T> extends AsyncTask<Void,Void,T>{
WeakReference<Activity> weakActivity
public SimpleBackgroundTask(Activity activity){
weakActivity = new WeakReference<Activity>(activity);
}
@Override
protected final T doInBackground(Void... voids) {
return onRun();
}
private boolean canContinue() {
Activity activity = weakActivity.get();
return activity != null && activity.isFinishing() == false;
}
@Override
protected void onPostExecute(T t) {
if(canContinue()) {
onSuccess(t);
}
}
abstract protected T onRun();
abstract protected void onSuccess(T result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment