如何处理来自搜索管理回调?回调、如何处理

2023-09-12 23:44:00 作者:粉兔兔

让我们假设如下:

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,因为我没有搜索管理意图(我想?)。

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

呼叫搜索管理器(在活动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);
            }
        });

在活动带把手的回电:

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 永远不会被发送到回调。为什么是这样?我假设,因为搜索Manager具有的意图,而不是活动B.我如何确保这个被调用?

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

推荐答案

我真的怀疑你是否需要调用一个新的意图贵就贵onclickListener主要活动。 由于该活动已经在这里的堆栈你可以完成它

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