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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:09:21+00:00 2026-05-24T07:09:21+00:00

I have two AsyncTask Activities, whose sole responsibilties are to execute an AsyncTask on

  • 0

I have two AsyncTask Activities, whose sole responsibilties are to execute an AsyncTask on the UI Thread. One of them works great, displays the progressBar, and updates the ProgressBar message. This is for my SearchActivity, which is actually a search activity with the Google Search metadata for declaring a search activity. I created a clone of the AsyncTask, and only changed doInBackground code, and also clone the SearchActivity and called it browseActivity.

I start BrowseActivity for result in a class that extends listView when an item is clicked. What I intended to happen was for the onPreExecute code of the AsyncTask to display a progressBar as the SearchActivity does, between the ListView activity, and the ResultActivity that the ListView will start onActivityResult. The onPreExcute code is being called but the ProgressBar is never displayed. (I thought I ran it on the UI thread correctly?)

Here is BrowseActivity:

AsyncTask<String, String, ArrayList<SearchResult>> browseTask;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
     String genreExtra = getIntent().getStringExtra(Genre.BUNDLE_TAG);
        if(genreExtra!=null){
            genre = Genre.getgenreFromExtra(genreExtra);
        }

        String categoryExtra = getIntent().getStringExtra(Category.BUNDLE_TAG);
        if(categoryExtra!=null){
            this.category = Category.getCategoryFromExtra(categoryExtra);
        }

        final Connector connector = GlobalVars.getCurrentConnector();

        Thread browseThread = new Thread(){

            @Override
            public void run() {
                try {
                    browseTask = connector.browse(BrowseActivity.this, category, genre, 1);
                    browseTask.execute("");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            };

            runOnUiThread(browseThread);
            ArrayList<SearchResult> result = null;

            try {
                result = browseTask.get();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Bundle queryBundle = new Bundle();
            queryBundle.putSerializable(SERIALIZABLE_KEY, result);
            Intent i = new Intent();
            i.putExtra(BUNDLE_KEY, queryBundle); //Put in our browse results
            i.putExtra(Category.BUNDLE_TAG, category.putExtra()); //put our category in return intent
            i.putExtra(Genre.BUNDLE_TAG, genre.putExtra()); //put our genre in return intent
            setResult(RESULT_OK, i);
            finish();

}

Here is the CategoryActivity (extends ListActivity) activity where browseActivity is started:

   @Override
    protected void onListItemClick(ListView l, View v, final int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(CategorySelectionView.this, BrowseActivity.class);
        final Category[] categories = Category.values();
        i.putExtra(Category.BUNDLE_TAG, categories[position].putExtra());
        i.putExtra(Genre.BUNDLE_TAG, genre.putExtra());
        CategorySelectionView.this.startActivityForResult(i, BrowseActivity.RESULT_REQUEST_CODE);
    }

 // Listen for results.
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        // See which child activity is calling us back.
        switch (requestCode) {
            case BrowseActivity.RESULT_REQUEST_CODE:
                // This is the standard resultCode that is sent back if the
                // activity crashed or didn't doesn't supply an explicit result.
                if (resultCode == RESULT_CANCELED){

                } 
                else {
                    ArrayList<SearchResult> recentQueryResults = (ArrayList<SearchResult>)data.getBundleExtra(
                            BrowseActivity.BUNDLE_KEY).getSerializable(BrowseActivity.SERIALIZABLE_KEY);
                    GlobalVars.setRecentQueryResults(recentQueryResults);
                    Category category = Category.getCategoryFromExtra(data.getStringExtra(Category.BUNDLE_TAG));
                    Intent i = new Intent(CategorySelectionView.this, RomListView.class);
                    i.putExtra(Category.BUNDLE_TAG, category.putExtra());
                    i.putExtra(Genre.BUNDLE_TAG, genre.putExtra());
                    startActivity(i);
                }
            default:
                break;
        }
    }
  • 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-24T07:09:22+00:00Added an answer on May 24, 2026 at 7:09 am

    Firstly, you can’t run an AsyncTask on the main/UI Thread, that’s the whole point. When you call browseTask.execute() its going to call its methods on the appropriate Thread (main vs background), regardless of calling runOnUiThread(). By calling runOnUiThread() all you are doing is wrapping the code that starts it in another Runnable. I think is not what you want since after calling runOnUiThread() you try to get the result of the task, which hasn’t run yet. The code in onCreate() runs on the UI thread, so this has to finish before something else can run, namely the onPreExecute() of the AsyncTask.

    What you should be doing is to create the AsyncTask and put everything you currently have after runOnUiThread() in onPostExecute().

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

Sidebar

Related Questions

tl;dr: I have two threads, one of them serealizes object, and another tries to
I have two questions: Can we start/execute an Async Task within a worker thread?
I have two classes, and want to include a static instance of one class
I have two threads, one needs to poll a bunch of separate static resources
I have two spreadsheets... when one gets modified in a certain way I want
I have two webapplication, one is a simple authenticationsite which can authenticate the logged
I have two spinners one named cuisine and other area. I m calling the
I have three simultaneous instances of an AsyncTask for download three files. When two
I have two activities; Home is my first activity and Settings is my second
In one of my Activities I do have up to six different AsyncTasks that

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.