Get result from SearchManager activity to Main Activity in android
Use Case: Activity A is main activity. Whereas Activity B is SearcActivity (SearchManager). Activity B is ListActivity, so whatever result comes, result is displayed in the list. Once user clicks on the list, I want to get that list selected item to be returned to ActivityA.
What I did? I could call SearchActivity on button using "onSearchRequested()". In ActivityB, I am displaying searchresult via "setListAdapter(adapter)". Using "onListItemClick", I can get what option in list was selected by user.
Now I tried sending result back to ActivityA by following code
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("item", l.getItemAtPosition(position).toString());
intent.putExtras(bundle);
setResult(RESULT_OK, getIntent() );
finish();
And in ActivityA, I tried reading the result in "onActivityResult".
Bundle bundle = data.getExtras();
String strItem = bundle.getString("item");
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Item Selected");
alertDialog.setMessage("Item = " + strItem);
alertDialog.setButton("OK", new DialogInterface.开发者_StackOverflow社区OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
Problem: I noticed that, once Activity A is resumed, "onActivityResult" is not called at all. Hence I am not seeing any dialog. Plus I can't verify whether SearchActivity actually sends anything to ActivityA.
So friends, how can I capture the data sent by ActivityB.
I think this is a duplicate to How to handle callback from Search Manager?.
Mario Lenci is right that your Activity B is not started with startActivityForResult
when calling onSearchRequested()
.
See my answer to that question here. Essentially you can instead make Activity A your searchable activity to get the query and manually launch Activity B for a result.
Is it you probably wanted to write:
setResult(RESULT_OK, intent );
instead of
setResult(RESULT_OK, getIntent() );
see the function call?
精彩评论