I have an Asynctask in my app that runs a process in the background to load a website and retrieve information from it. During this I show a loading dialog.
While the process is working, I show an interstitial (full screen) ad.
EDIT: When this ad is shown (not necessarily when the x is pressed), the app cancels my Asynctask.
I'd like the ad to not cancel my Asynctask.
private class LongOperation extends AsyncTask<Object, Void, String> {
ProgressDialog dialog;
@Override
protected String doInBackground(Object... params) {
try {
Log.d(TAG, "Parsing website...");
ArrayList<String> links = new ArrayList<String>();
links = pullLinks((String) params[1]);
Configuration config = new Configuration();
config.setLocalStoragePath((String) params[0]);
// Get desktop versions only
config.setBrowserUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4");
Goose goose = new Goose(config);
Article article = goose.extractContent(links.get(0));
return article.cleanedArticleText();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
@Override
protected void onPostExecute(String webText) {
// Allow rotation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Log.i(TAG, "EXECUTED");
if (webText.equals(""))
handleSendText(UNSUPPORTED_WEBSITE);
else
handleSendText(webText);
dialog.dismiss();
super.onPostExecute(webText);
}
@Override
protected void onPreExecute() {
lockOrientation();
dialog = ProgressDialog.show(PasteActivity.this, "Add",
"Getting article from website, please wait...", true, true,
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
dialog.dismiss();
LongOperationTask.cancel(true);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
});
dialog.setCancelable(false);
}
@Override
protected void onCancelled() {
Toast.makeText(PasteActivity.this, "Adding cancelled.",
Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
Another edit: Adding:
dialog.setCanceledOnTouchOutside(false);
Does not fix this either.
Edit: Removing:
LongOperationTask.cancel(true);
Does not fix this either.
Edit: So I realized that the interstitial ads actually call onPause() on my activity. And in my onPause I cancel my task. I'm attempting to move the cancellation of the task to onStop() instead. But this isn't ideal.
Update: Does not fix this either. But the task appears to keep running once I cancel the ad. However a few seconds later it is still cancelled.