Android onActivityResult 始终为 0

发布于 2024-10-09 02:34:05 字数 2036 浏览 2 评论 0原文

这两天已经让我心烦意乱了。我有一个主活动 A,它调用第二个活动 B。活动 B 只是向用户呈现一个列表视图。当我按下列表视图上的某个项目时,我希望将几个字符串传递回主 Activity A,然后 Activity B 将完成。

问题是我总是得到结果代码 0 并且数据包为空。我真的不明白为什么会发生这种情况。

这是我的代码。

启动活动 B 以获取结果:

Test.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(recipeActivity.this, BrowseLoadRecipes.class);
            startActivityForResult(i, RECIPE_CHOOSER);
    }  
    });

这将启动第二个活动。活动 B 填充列表视图,当我单击某个项目时,我尝试将一些数据发送回调用活动 A。

此时有任何文本,因此我在活动 B 中使用了以下内容:

     lv.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        Bundle bundle = new Bundle();
        bundle.putString("TEXT", "Please work... pleeeeaasee");
        Intent mIntent = new Intent();
        mIntent.putExtras(bundle);
        setResult(RESULT_OK, mIntent);
        finish();
     }
     });

在调用活动中,我有以下监听返回如下:

protected void onActivityResult(int requestCode, int resultCode, 
        Intent data) { 
            switch(requestCode) { 
            //TODO
            case RECIPE_CHOOSER:
                Toast.makeText(getApplicationContext(), "In recipe return", Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "resultCode is " + String.valueOf(resultCode), Toast.LENGTH_SHORT).show();
                if (resultCode == RESULT_OK) {
                Bundle b = getIntent().getExtras();

                Toast.makeText(getApplicationContext(), "Returned " + b.getString("TEXT"), Toast.LENGTH_LONG).show();
                }
                if (resultCode == RESULT_CANCELED) {

                    }
                break;

                } 
            } 
        }

可以看到请求code正确返回,但是resultcode始终为0,数据始终为null。

我已经完成了调试,setResult 正在执行其工作,并且捆绑包确实具有我正在传递的数据,但它在途中的某个时刻丢失了。

清单中是否有我遗漏的内容或其他内容。到目前为止,它已经扼杀了我在这个项目上的进展。

This has been killing me for two days now. I have a main Activity A which calls a second Activity B. Activity B simply presents the user with a listview. When I press an item on the list view I want a couple of strings to be passed back to the main Activity A and Activiy B will finish.

The problem is I always get a resultcode of 0 and the data bundle is null. I really don't understand why this is happening.

Here is my code.

Start Activity B for result:

Test.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(recipeActivity.this, BrowseLoadRecipes.class);
            startActivityForResult(i, RECIPE_CHOOSER);
    }  
    });

This starts the second Activity fine. Activity B populates a listview and when I click an item I'm trying to send some data back to the calling Activity A.

Any text at the moment, so I used the following in Activity B:

     lv.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        Bundle bundle = new Bundle();
        bundle.putString("TEXT", "Please work... pleeeeaasee");
        Intent mIntent = new Intent();
        mIntent.putExtras(bundle);
        setResult(RESULT_OK, mIntent);
        finish();
     }
     });

In the calling activity I have the following listening for the return as follows:

protected void onActivityResult(int requestCode, int resultCode, 
        Intent data) { 
            switch(requestCode) { 
            //TODO
            case RECIPE_CHOOSER:
                Toast.makeText(getApplicationContext(), "In recipe return", Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "resultCode is " + String.valueOf(resultCode), Toast.LENGTH_SHORT).show();
                if (resultCode == RESULT_OK) {
                Bundle b = getIntent().getExtras();

                Toast.makeText(getApplicationContext(), "Returned " + b.getString("TEXT"), Toast.LENGTH_LONG).show();
                }
                if (resultCode == RESULT_CANCELED) {

                    }
                break;

                } 
            } 
        }

I can see that the request code is correctly returned, but the resultcode is always a 0 and the data is always a null.

I've ran through the debug and the setResult is doing its job and the bundle does indeed have the data I'm passing, but it's lost at some point along the way.

Is there something in the manifest I'm missing or something. It's killed my progress on this project so far.

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

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

发布评论

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

评论(3

赏烟花じ飞满天 2024-10-16 02:34:05

在您的列表活动中, onItemClickListener 尝试以下操作,将 setResult 行替换为:

if (getParent() == null) {
    setResult(Activity.RESULT_OK, data);
}
else {
    getParent().setResult(Activity.RESULT_OK, data);
}

我想知道是否有一个父活动,您需要将数据绑定到该父活动并将结果设置为......

In your list activity, onItemClickListener try the following replacing the setResult lines with:

if (getParent() == null) {
    setResult(Activity.RESULT_OK, data);
}
else {
    getParent().setResult(Activity.RESULT_OK, data);
}

I'm wondering whether there is a parent activity which is what you need to bind the data to and set the result on....

草莓味的萝莉 2024-10-16 02:34:05

关于您返回的数据。

你这样做:

Bundle b = getIntent().getExtras();

但是“getIntent()”返回启动此活动的意图。
如果您想要从您启动的 Activity 返回的数据作为结果,只需采取
传递给的数据

protected void onActivityResult(int requestCode, int resultCode, Intent data)

Concerning your returned data.

You do:

Bundle b = getIntent().getExtras();

but "getIntent()" returns the Intent that started THIS activity.
If you want the returned data from the Activity you started for result, just take
the data which is passed to

protected void onActivityResult(int requestCode, int resultCode, Intent data)
辞旧 2024-10-16 02:34:05

您的代码运行完美......

in u Activity B
use
  setResult(0, mIntent);insted of setResult(RESULT_OK, mIntent);
in your Activity A:
use
case 0: insted case RECIPE_CHOOSER: and 
use System.out.println(b.getString("TEXT"));

您将得到输出为

请工作...pleeeeaasee

Your code is perfectly working........

in u Activity B
use
  setResult(0, mIntent);insted of setResult(RESULT_OK, mIntent);
in your Activity A:
use
case 0: insted case RECIPE_CHOOSER: and 
use System.out.println(b.getString("TEXT"));

You will get output as

Please work... pleeeeaasee

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