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

  • SEARCH
  • Home
  • 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 5838085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:25:49+00:00 2026-05-22T11:25:49+00:00

The Problem is: My Activity starts an AsyncTask in onStart(). In the doInBackground Method

  • 0

The Problem is:

My Activity starts an AsyncTask in onStart().
In the doInBackground Method I make a short webrequest, and depending on your network connetion, this may take a little, so I want this task cancelable…

BUT..

After hours of searching, testing and debugging I noticed now, when the user presses the back button during the doInBackground Method, the Keyevent ist always dispatched AFTER my doInBackground method is finished.

So I dont have to wonder why the asynctask never is cancelled when the users presses the backbutton,
the AsyncTask.cancel(true) is invoked too late….. (even if I am not sure if cancel(true) will help)

So is this normal behavoiur with asynctask and backbutton?

This cant be normal, because how should the user ever get back from the activity on slow connection?

wait for timeout?

I am Begging for Help, cancel an async webrequest SHOULD be possible 🙂

  • 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-22T11:25:50+00:00Added an answer on May 22, 2026 at 11:25 am

    when the user presses the back button during the doInBackground Method, the Keyevent is always dispatched AFTER my doInBackground method is finished.

    No, this isn’t true.

    If I hit the BACK button on my device when an AsyncTask is running in my main Activity (downloading files from my server), the Activity is immediately closed.

    What IS true, however, is the AsyncTask will continue to run whatever code is in its doInBackground() method UNLESS I explicitly cancel it (but you kind of know that already).

    As far as I can tell, your ‘webrequest’ (whatever that may be) is blocking your doInBackground() method and because of that, any attempt to cancel it in onPause(), onStop, onDestroy() etc will never work.

    As advantej points out, the way the AsyncTask.cancel(...) method works is that it causes isCancelled to be set to ‘true’. In order to successfully cancel the AsyncTask’s doInBackground() method you need to periodically check isCancelled. Example…

    @Override
    protected Void doInBackground(String... params) {
    
        while (!isCancelled) {
            DoSomething();
        }
    }
    

    The problem is if DoSomething() (for example your ‘webrequest’) is blocking the while loop then isCancelled won’t be checked until it completes.

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

Sidebar

Related Questions

I have an Activity which starts an AsyncTask. This AsyncTask uses AutoCompleteTextView.showDropDown() in onPostExecute.
I have a problem with my activity. This is my onCreate: case NORMAL: game
When I press the button new activity starts. In this activity pop-ups alertdialog with
I have an activity that starts with an AsyncTask to check the connection to
I have the following problem: I have an Activity where a user can start
Here I'm gettimg memory allocation problem at activity indicator and my code is: -
I have a service that also contains an activity the problem is sometimes it
I have a problem to test my non activity-class which need the context of
I have the following problem: I opened the activity A, from which I opened
My problem is how to switch from context menu to correct activity. I have

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.