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 8128863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:04:13+00:00 2026-06-06T08:04:13+00:00

When I log in into my app, an async task starts executing and while

  • 0

When I log in into my app, an async task starts executing and while that task is being executed and I logout of app, that task is still running and give me the results after sometime(though I have logged out). I want to ask, is there any way to cancel that task so that it doesn’t give me results?

  class AsyncClass extends AsyncTask<>{

    @Override
     protected String doInBackground(Void... params)
      {
             if(isCancelled())
               {
                Log.d("isCancelled", iscancelled());
               }
          //call the webservice

      }
    }

Now there is some other class from where I’m calling

if(asyncTaskObject!=null){
    asyncTaskObject.cancel(true);
        asyncTaskObject=null;
}

But Log statement inside iscancelled() is never called.

  • 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-06-06T08:04:18+00:00Added an answer on June 6, 2026 at 8:04 am

    I had the same problem just a day ago 🙂

    A mixture of the 3 other answers that works for me.

    First declare your asyncTask on a field:

    private MyTaskClass miTask;
    

    On the onCreate/onResume if an activity:

    miTask = new MyTaskClass();
    

    Then you can execute it in any method.

    miTask.execute();
    

    And in the onPause/onStop:

    miTask.cancel(true);
    

    This will only work if in your doInBackground you check isCancelled(), an example that i made for a cursor access that was already close if the fragment was dismissed:

    @Override
        protected Void doInBackground(Void... params) {
            cached = true;
            int idIndex = currentCursor.getColumnIndex(Contacts._ID);
            int displayNameIndex = currentCursor
                    .getColumnIndex(Contacts.DISPLAY_NAME);
    
            while (currentCursor.moveToNext()) {
                if (isCancelled()) {
                    break;
                }
    

    Hope that helps, regards.
    Alex

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

Sidebar

Related Questions

I'm trying to create an app that multiple users would log into a server
Hey I'm building an app that will allow a user to log into a
When the user successfully log into my app, session starts, after X period of
I have a web app that you can currently log into with either your
I'd like my web app to log into files with this path: webapp/logs/ I
When I log into my app, my browser's cookie for the same site is
I'm developing a Mac App in Java that logs into any one of our
When my app start it should promt user to log into gaming center so
I am building a website/app using node.js, express and nowjs. Once user log into
I am working on an app which shall log in to a web site

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.