Last active
December 10, 2015 14:38
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onDestroy() { | |
if (mAsyncTask != null && mAsyncTask.getDialog() != null | |
&& mAsyncTask.getDialog().isShowing()) | |
mAsyncTask.getDialog().dismiss(); | |
super.onDestroy(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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