Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6598055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:17:25+00:00 2026-05-25T18:17:25+00:00

I have this AsyncTask: private class GetMyFlights extends AsyncTask<String, Void, Integer> { private ProgressDialog

  • 0

I have this AsyncTask:

private class GetMyFlights extends AsyncTask<String, Void, Integer> {       
    private ProgressDialog dialog;  

    public GetMyFlights(ListActivity activity) {}           

    @Override
    protected Integer doInBackground(String... params) {
        return getData();
    }       

    @Override
    protected void onPostExecute(Integer result) {  
        super.onPostExecute(result);    
        //...
    }
}

When I change to another activity I want to stop it. So I set this:

@Override
protected void onPause() {
    if(mGetMyFlights != null){
        mGetMyFlights.cancel(true);
        Log.d(TAG, "MyFlights onPause, cancel task");
    }
    super.onPause();
}

But the code inside getData is still working when I change my activity. How can I be sure is stop?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:17:26+00:00Added an answer on May 25, 2026 at 6:17 pm

    What I usually do is call cancel(true) on the AsyncTask instance and check Thread.interrupted() in doInBackground. You could check isCancelled(), but that doesn’t work if the actual work is done in some other class that is independent from your AsyncTask and doesn’t know about it. For example (copied directly from my own getData() method, inside my Data class, independent from any activities, async tasks, etc.):

    while ((line = reader.readLine()) != null) {
        if (Thread.interrupted()) throw new InterruptedException();
        content.append(line);
    }
    

    Just make sure to handle the InterruptedException in your doInBackground(), e.g.:

    @Override
    protected Integer doInBackground(String... params) {
        try {
            return getData();
        }
        catch (InterruptedException e) {
            Log.d("MyApp", "Girl, Interrupted");
            return -1;
        }
    }
    

    Also worth noting that onPostExecute() is not called if the task is cancelled. Instead, onCancelled() is called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code fragment: public static class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
I have created an asynchronous task like this: private class LongOperationcheckall extends AsyncTask<String, String,
private void Load() { new pbar().execute(); //dialog = ProgressDialog.show(Display.this, , Loading. Please wait..., true);
I have a class that extends Activity and within it got a private class
I have a common class say for eg Class A which extends AsyncTask and
I am trying to execute the following AsyncTask : private class TimeUpdateTask extends AsyncTask<List<mObject>,
I have a class which extends AsyncTask, which is intended to serve as a
I have a public class FriendMaps extends MapActivity that gets called from a menu
friends, i am using following code inside asyncTask public class AsycLoaderFromDbAndMapInjector extends AsyncTask {
I have a backgorund thread that extends AsyncTask and which I use in activity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.