How to handle callback from Search Manager?

2019-02-19 14:37发布

问题:

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?

回答1:

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();
            }
        });


回答2:

I posted an answer to what I think is the same problem at Android onSearchRequested() callback to the calling activity

Skythe is right that onActivityResult isn't called because Activity A isn't starting the search activity for a result.

To work around this, I made Activity A also defined as the searchable actvity (in the manifest), so that it receives the search intent and then manually launch Activity B with startActivityForResult, passing in the query and letting Activity B handle the search as normal.

You can see my response at the link above or I have a more detailed write-up in this blog post.



回答3:

onActivityResult isn't called because startActivityForResult isn't used to start the search activity. I'm stuck with this problem as well and have yet to find a solution!