Skip to content

Instantly share code, notes, and snippets.

@RobGThai
Last active August 29, 2015 14:19
Show Gist options
  • Save RobGThai/cc222a41ab699e7eff3c to your computer and use it in GitHub Desktop.
Save RobGThai/cc222a41ab699e7eff3c to your computer and use it in GitHub Desktop.
Abstract class for helping with naming AsyncTask in Android.
package common.asynctask;
import android.os.AsyncTask;
public abstract class NamedAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
private String oldName;
private void setNewThreadName(String name) {
oldName = Thread.currentThread().getName();
if(!TextUtils.isEmpty(name)) Thread.currentThread().setName(name);
}
protected abstract String getThreadName();
@Override
protected void onPreExecute() {
super.onPreExecute();
setNewThreadName(getThreadName());
}
@Override
protected void onCancelled() {
Thread.currentThread().setName(oldName);
super.onCancelled();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment