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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:52:45+00:00 2026-06-16T13:52:45+00:00

I have an asynctask that does something, when its done, I want it to

  • 0

I have an asynctask that does something, when its done, I want it to broadcast that its done.

usually I would do : context.sendBroadcast(new Intent(MYINTENT)); however asynctask has no context. I’ve seen a few answers to this questions suggesting sending a reference to the context of the app’s activity to the asynctask. but that reference is bad if the user rotates the screen. and manually maintaining the reference is a bad solution (requires too much from the activity creating the asynctask, which I do not control). now the questions are:

1) why is android set up like that ? why do I even need a context to send a broadcast when broadcasts can be registered for and handled by other contexts ?

2) is there a good solution to this problem ? (good = requires as little as possible from the activity creating the asynctask, survives rotations, etc..).

  • 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-16T13:52:49+00:00Added an answer on June 16, 2026 at 1:52 pm

    The context that you are using in your AsyncTask right now is the context of your current Activity. By default a screen rotation will destroy the current instance of that Activity and create a new one.
    This is (even if it may not seem so at first) intended behavior. The reason for this is that you may want to have different resources (layouts, drawables, etc) for different screen orientations. In order to apply those potentially different resources Android will recreate the Activity on every rotation.

    You can counteract that by setting the android:configChanges attribute in your AndroidManifest.xml file but in your case this solution is not recommended.

    The proper way of dealing with this problem is to pass the Application Context to the AsyncTask instead of your Activity (Activity inherits from Context). You can do that by calling getApplicationContext() from the instance of your Activity.
    Your applications context will persist events such as screen rotation and lives until the system kills the app.

    As to why you need an instance of Context to do basic tasks:

    Interface to global information about an application environment. This
    is an abstract class whose implementation is provided by the Android
    system. It allows access to application-specific resources and
    classes, as well as up-calls for application-level operations such as
    launching activities, broadcasting and receiving intents, etc.

    This is how the official documentation defines a Context.
    Maybe someone can explain this better but for myself this definition is sufficient.

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

Sidebar

Related Questions

I have a AsyncTask that runs and does its job and completes. onPostExecute does
I have an AsyncTask and on the background I want to start Service that
I have an AsyncTask that queries a content provider and does some additional processing
I have some code that does something like this: abstract class Data { Data(string
I have method that does asynchronous call to web service. Something like that: public
I have an application that does some long calculations, and I would like to
My goal is to have an AsyncTask that can execute multiple times (one task
I have a class that extends AsyncTask , which fetches the gps cordinates from
I have a class that extends AsyncTask. In the doInBackground() method, I connect to
I have an Activity class which has an attribute that references an AsyncTask instance

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.