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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:27:13+00:00 2026-05-25T21:27:13+00:00

I am trying to implement the following logic: User enters a search string in

  • 0

I am trying to implement the following logic:

User enters a search string in a text box (an activity) -> Web service is called to perform the search asynchronously -> Search results are displayed in a list (another activity)

So far I have the following:

An activity to enter the search string and click a “Search” button:

public class Search extends Activity
{
    // ...
    // called when "Search" button is clicked
    private void runSearch()
    {
        ProgressDialog progressDialog = ProgressDialog.show(
            this, "Search", "Search...");
        searchAsyncTask = new SearchAsyncTask();

        SearchAsyncTaskParam param = new SearchAsyncTaskParam();
        param.SearchString = getSearchCode(); // gets input from text box
        param.ProgressDialog = progressDialog;

        searchAsyncTask.execute(param);
    }
}

Then I have a class to perform the asynchronous search:

public class SearchAsyncTask extends AsyncTask<SearchAsyncTaskParam,
    Void, SearchAsyncTaskResult> {

    private SearchAsyncTaskParam param;

    @Override
    protected SearchAsyncTaskResult doInBackground(
        SearchAsyncTaskParam... params) {
        if (params.length > 0)
            param = params[0];
        SearchAsyncTaskResult result = new SearchAsyncTaskResult();

        // call Webservice and fill result object with status (success/failed)
        // and a list of hits (each hit contains name, city, etc.)

        return result;
    }

    @Override
    protected void onPostExecute(SearchAsyncTaskResult result) {
        param.ProgressDialog.dismiss();
        if (!result.Success)
            // throw an AlertBox
        else
        {
            // this part is incomplete and doesn't look good, does it?
            // And how would I pass my result data to the new activity?
            Intent intent = new Intent(param.ProgressDialog.getContext(),
                SearchResultList.class);
            param.ProgressDialog.getContext().startActivity(intent);
        }
    }
}

And the last element is an activity to display a list with the search results:

public class SearchResultList extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(
            this, android.R.layout.simple_list_item_1, data));
        // String was only for testing, should be a class with holds the data
        // for each item, i.e. Name, City, etc. And where do I get "data" from?
    }
    // ...
}

Now my questions:

  1. Is it a good idea to start the activity for the result list in the onPostExecute method of the AsyncTask instance? I have tested it with the simple test sketched above and it seems to work. But is this safe, am I on the right thread (UI thread) at all? If this is bad practice what is the alternative to start the result activity?

  2. It looks especially weird to use the context of the ProgressDialog for the Intent and to start the activity. It is just the only context I have available in the AsyncTask instance. Can this be a problem because the ProgressDialog is already dismissed? What context else could I use?

  3. How do I pass my result data into the SearchResultList activity?

  4. In case of a successful search (the else case in the onPostExecute method above) I would like to close the Search activity with the text box so that, if the back button is hit, the user returns to the main screen (which the search activity was opened from) and not the Search activity. Is this possible and how can I do that?

  • 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-25T21:27:14+00:00Added an answer on May 25, 2026 at 9:27 pm
    1. Yes, because you are on the UI thread. The alternative would be if you aren’t using AsyncTask but just a simple thread. In that case you would use a Handler to notify the UI thread that work is complete. This is one of the reasons Asynctask exists, so you don’t have to do the latter version.

    2. “It looks especially weird to use the context of the ProgressDialog
      for the Intent and to start the activity.”

      It’s not weird at all because when you created the ProgressDialog you passed the context of Search, so when you retrieve the context of the ProgressDialog you actually get the context of the Search activity. No problem at all.

    3. This depends on what data you want to pass. If you just want to pass an array of Strings you could do it like this:

      String [] data = getData();
      Intent intent = new Intent(param.ProgressDialog.getContext(),
          SearchResultList.class);
      intent.putExtra("key1", data);
      param.ProgressDialog.getContext().startActivity(intent);
      

      Then in the SearchResultList activity you would do:

      String [] data = this.getIntent().getStringArrayExtra("key1");
      

      If you would like to pass an object of your own, see this question: How to declare global variables in Android?

    4. Is it possible? Yes. There’s two ways to do this in your case. Either you can, as I would do it, write the SearchAsyncTask as a inner class in your Search class and the just call

      Search.this.finish();
      

      If some condition holds, or you can check out the accepted anwser to this question: android how to finish an activity from other activity

    Good Luck!

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

Sidebar

Related Questions

I am trying to implement security for the following architecture: Web tier: Tomcat 7
Im trying to implement the following logic on javascript. If type is 'bankAccountTypeId' get
I'm trying to implement the following mod_rewrite rule: host.com/developer/ => host.com/developer/index.py host.com/developer/branchX => host.com/developer/index.py?branch=branchX
I'm trying to implement routing such as the following: posts/535434/This-is-a-post-title posts/tagged/tags+here // Matches {controller}/{action}/{id}
I'm trying to implement XOR in javascript in the following way: // XOR validation
I'm trying to implement my own GenericIdentity implementation but keep receiving the following error
I'm trying to implement http://github.com/bengottlieb/Twitter-OAuth-iPhone/tree/master into my code. I'm following the Demo provided and
I'm having the following issue in a WinForms app. I'm trying to implement Hotkeys
trying to implement a dialog-box style behaviour using a separate div section with all
I would like to implement the following logic against Entity Frameworks. var items =

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.