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

发布于 2024-09-16 19:46:37 字数 1983 浏览 3 评论 0原文

让我们假设以下情况:

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

onActivityResult() 永远不会被调用。为什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别想她 2024-09-23 19:46:37

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

Skythe 是正确的,onActivityResult 没有被调用,因为活动 A 没有启动结果的搜索活动。

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

您可以在上面的链接中查看我的回复,或者我有一个 这篇博文中有更详细的描述

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.

安静被遗忘 2024-09-23 19:46:37

我真的怀疑您是否需要在 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();
            }
        });

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();
            }
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文