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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:50:34+00:00 2026-06-02T01:50:34+00:00

When using the @Background annotation we start a new thread. And if we while

  • 0

When using the @Background annotation we start a new thread. And if we
while this thread is executing where to rotate the screen, will we
then lose the callback from that thread or how is that handled? With
the Loaders this is sorted out behind the screen so we don’t have to
worry about the problems that occurred frequently back when we used
async tasks.

But how does the @Background annotation deal with this ?

  • 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-02T01:50:36+00:00Added an answer on June 2, 2026 at 1:50 am

    First of all, when you use the @Background annotation, the code is executed on a separate thread, but this doesn’t necessarily mean that a new thread will be started, because we use a common thread pool (which can be replaced) for all @Background methods.

    Like an AsyncTask, @Background does not handle any lifecycle changes on your activities. So, if you call an @Background method and then the screen is rotated, then the @Background code will be executed, no matter what, on the instance it was called on. If @Background is placed on a method that belongs to the activity, and in turns call a @UiThread method, there is a risk that the @UiThread method will be called on the wrong activity instance, if a configuration change occurred.

    In Android, prior to Loaders, the usual way to handle that was to use AsyncTask, keep references to those tasks in onRetainNonConfigurationInstance(), and rebind them to the new activity after a config change.

    In the latest release, AndroidAnnotations provides the @NonConfiguration instance annotation, which can be combined with @EBean / @Bean and @Background to achieve the same effect.

    Here is a sample code (not tested, written from gmail) :

    @EActivity
    public class MyActivity extends Activity {
    
      // Using @NonConfigurationInstance on a @Bean will automatically update the context ref on configuration changes (if the bean is not a singleton)
      @NonConfigurationInstance
      @Bean
      MyBackgroundTask task;
    
      @Click
      void myButtonClicked() {
        task.doSomethingInBackground();
      }
    
      void showResult(MyResult result) {
        // do something with result
      }
    
    }
    
    
    @EBean
    public void MyBackgroundTask {
    
      @RootContext
      MyActivity activity;
    
      @Background
      void doSomethingInBackground() {
          // do something
         MyResult result = XXX;
    
          updateUI(result);
      }
    
      // Notice that we manipulate the activity ref only from the UI thread
      @UiThread
      void updateUI(MyResult result) {
        activity.showResult(result);
      }
    }
    

    I think we could provide even better solutions, but there are a lot of different use cases, and we need to think about them all. So right now, @Background has a very simple behavior and I don’t want to change that. We could, however, introduce new annotations with an advanced “thread + lifecycle” behavior.

    Thanx to “Pierre-Yves Ricau” for providing this answer via the googlegroup for androidannotations. Hope this will help others who might get stuck with a similar problem.

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

Sidebar

Related Questions

what is the difference between create thread using thread.start and using background worker ?
I'm trying to create a new spinner background using a 9 patch picture. I've
I'm trying to set an image as the background using this code: root.setStyle(-fx-background-image: url('splash.jpg');
From what I understand, if you're using the mvc:annotation-driven tag, then you can pass
View model is loading data asynchronously using background worker thread in model. All properties
I have this jquery issue that seems to only break when using background image
I am using Background Thread to update one of my label I am using
Using this: http://jquery-ui.googlecode.com/svn/branches/labs/selectmenu/index.html I'm using background images instead of gradients/plain colours. I want to
Background Using boost and other similar libraries is the easiest way to find compiler
Background: Using jQuery 1.7 client side PHP server side Using json responses with json_encode

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.