Android onActivityResult 始终为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的列表活动中, onItemClickListener 尝试以下操作,将 setResult 行替换为:
我想知道是否有一个父活动,您需要将数据绑定到该父活动并将结果设置为......
In your list activity, onItemClickListener try the following replacing the setResult lines with:
I'm wondering whether there is a parent activity which is what you need to bind the data to and set the result on....
关于您返回的数据。
你这样做:
但是“getIntent()”返回启动此活动的意图。
如果您想要从您启动的 Activity 返回的数据作为结果,只需采取
传递给的数据
Concerning your returned data.
You do:
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
您的代码运行完美......
您将得到输出为
Your code is perfectly working........
You will get output as