Skip to content

Instantly share code, notes, and snippets.

@indrek-koue
Last active December 10, 2015 14:38
Show Gist options
  • Save indrek-koue/4448446 to your computer and use it in GitHub Desktop.
Save indrek-koue/4448446 to your computer and use it in GitHub Desktop.
AsyncTask base broilerplate NB! override OnDestroy in activity in order to avoid leaked window exceptions
@Override
protected void onDestroy() {
if (mAsyncTask != null && mAsyncTask.getDialog() != null
&& mAsyncTask.getDialog().isShowing())
mAsyncTask.getDialog().dismiss();
super.onDestroy();
}
package com.modera.taxofondriver.base;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
public abstract class BaseAsyncTask<T1, T2, T3> extends AsyncTask<T1, T2, T3> {
private ProgressDialog mDialog;
private String mDialogTitle;
private String mDialogMessage;
private Activity a;
@Override
protected void onPreExecute() {
if (a == null)
throw new RuntimeException("BaseAsynctask Activity is null");
mDialog = ProgressDialog.show(a, mDialogTitle, mDialogMessage, true,
true, new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
cancel(true);
}
});
}
protected void onPostExecute(T3 result) {
if (a != null)
mDialog.dismiss();
}
// getters and setters
public void setDialogTitle(String mDialogTitle) {
this.mDialogTitle = mDialogTitle;
}
public String getDialogTitle() {
return mDialogTitle;
}
public void setDialogMessage(String mDialogMessage) {
this.mDialogMessage = mDialogMessage;
}
public String getDialogMessage() {
return mDialogMessage;
}
public void setActivity(Activity a) {
this.a = a;
}
public Activity getActivity() {
return a;
}
public ProgressDialog getDialog() {
return mDialog;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment