让我们假设以下内容:
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
我无法处理从活动 B 到活动 A 的回调,因为我没有搜索管理器意图(我认为?)。
呼叫搜索管理器(在活动 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);
}
}
用户选择列表项后返回活动 A
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);
}
});
在活动 A 中处理回调:
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();
}
}
}
PICK_COMPANY_REQUEST
永远不会发送到回调。为什么是这样?我假设是因为搜索管理器有意图,而不是 Activity B。我怎样才能确保它被调用?
onActivityResult() 永远不会被调用。为什么?