0

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.

Doronz
  • 704
  • 8
  • 21
  • I believe this question would benefit from showing more code, at least the lifecycle callbacks from your activity so other members can figure out what is going on. Also, your ad [shouldn't cause onPause() to be called](http://stackoverflow.com/questions/7240916/android-under-what-circumstances-would-a-dialog-appearing-cause-onpause-to-be), isn't that right? – Piovezan Apr 21 '14 at 23:36

1 Answers1

1

I believe your AsyncTask is linked to your Activity (your AsyncTask instance is probably an attribute of your Activity - otherwise show us how they are related to each other so we can help out). Thus when you recreate your Activity (e.g. by doing setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)) the AsyncTask reference is lost (garbage-collected) and it stops running.

My suggestion in order to keep the AsyncTask running throughout orientation changes is to follow the pattern shown in this sample project (the way it is done is a bit outdated though, in the case you are using Fragments, but it is simple and works).

Piovezan
  • 3,215
  • 1
  • 28
  • 45
  • Actually even forcing orientation to portrait only (i.e. no rotation possible), the task gets cancelled while the ad is showing. – Doronz Apr 21 '14 at 22:58