2

让我们假设以下内容:

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() 永远不会被调用。为什么?

4

2 回答 2

5

我在调用活动的 Android onSearchRequested() 回调中发布了我认为相同问题的答案

SkytheonActivityResult没有被调用是对的,因为活动 A 没有启动搜索活动以获取结果。

为了解决这个问题,我将 Activity A 也定义为可搜索的 Activity(在清单中),以便它接收搜索意图,然后使用 手动启动 Activity B startActivityForResult,传入查询并让 Activity B 正常处理搜索。

你可以在上面的链接中看到我的回复,或者我在这篇博文中有更详细的文章

于 2011-05-11T18:16:57.047 回答
0

我真的怀疑您是否需要在 onclickListener 上为 Main 活动调用新意图。由于活动已经在堆栈中,您可以完成它

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();
            }
        });
于 2010-08-30T06:28:53.713 回答