Search code examples
androidandroid-activityandroid-intentandroid-searchmanager

How to handle callback from Search Manager?


Let's assume the following:

Activity A calls Search Manager
User searches, and search results are displayed in Activity B
User clicks on a list item in Activity B 
App switches back to Activity A

I am unable to handle this callback from Activity B to Activity A because I don't have the Search Manager intent (i think?).

Call Search Manager (in Activity A)

public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.add_symbol:
            onSearchRequested(); //result of search will show Activity B
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

Go back to Activity A after user has selected a list item

listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                Quote myQuote = new Quote();
                myQuote.name = (companies.get(position).name);
                myQuote.symbol = (companies.get(position).symbol);

                //TODO: add new quote to master quote list in Main
                //TODO: serialize in Main
                //TODO: go back to last activity

                Intent myIntent = new Intent(getApplicationContext(), Main.class);
                startActivityForResult(myIntent, PICK_COMPANY_REQUEST);
            }
        });

In Activity A handle the call back:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PICK_COMPANY_REQUEST) {
            if (resultCode == RESULT_OK) {
                Toast toast = Toast.makeText(getApplicationContext(), "Stock added", Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    }

The PICK_COMPANY_REQUEST is never sent to the call back. Why is this? I am assuming because the Search Manager has the intent, and not Activity B. How can I make sure this gets invoked?

onActivityResult() is never called. Why?


Solution

  • I really doubt whether you need to call a new intent for your Main activity on your onclickListener. Since the activity is already here in the stack you can just finish it

    listView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                    Quote myQuote = new Quote();
                    myQuote.name = (companies.get(position).name);
                    myQuote.symbol = (companies.get(position).symbol);
    
                    //TODO: add new quote to master quote list in Main
                    //TODO: serialize in Main
                    //TODO: go back to last activity
    
                   //You need to use setresult here and pass the intent
                    setResult(RESULT_OK, myIntent );
                    finish();
                }
            });