startActivityForResult(Intent intent,int requestCode,Bundle options)如何获取额外的bundle选项? [英] startActivityForResult(Intent intent, int requestCode, Bundle options) how to retrieve the extra bundle option?
问题描述
According to the Android documentation public void startActivityForResult (Intent intent, int requestCode, Bundle options).
我不知道如何检索我打算传递的额外捆绑选项.
I can not figure it out how to retrieve the extra bundle options that I am passing on the intent.
当我调用startActivityForResult方法时,我想传递一个带有数据的ArrayList作为附加包选项.
I want to pass an ArrayList with data as an extra bundle option when I am calling the startActivityForResult method.
代码示例:
ArrayList<String> list = new ArrayList<>();
list.add("test1");
list.add("test2");
Bundle bundleOptions = new Bundle();
bundleOptions.putStringArrayList("key", list);
startActivityForResult(intent, 10, bundleOptions);
检索数据后:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
Bundle extras = data.getExtras();
附加捆绑包不包含我尝试传递的附加捆绑包.我丢失了什么,我无法检索传递给该方法的额外捆绑包数据?
Bundle extras does not contain the extra bundle that I am trying to pass. What I am missing and I can not retrieve the extra Bundle data that I am passing to the method?
我也尝试了intent.putExtra("key", bundleOptions);
,也尝试了intent.putExtra("key", list);
,但均未成功,并调用了方法startActivityForResult(intent, 10);
,但再次均未成功.
I also tried with intent.putExtra("key", bundleOptions);
, also with intent.putExtra("key", list);
but with no success either and calling the method startActivityForResult(intent, 10);
but again with no success.
我确定我错过了有人知道如何实现这一目标吗?
I am sure that I am missing something does anybody know how to achieve this?
推荐答案
额外捆绑包不包含我要传递的额外捆绑包.
Bundle extras does not contain the extra bundle that I am trying to pass.
正确.
我想念的东西
What I am missing
如果您希望使用getExtras()
检索值,请使用putExtras()
或单独的putExtra()
方法.
If you wish to retrieve a value using getExtras()
, use putExtras()
or individual putExtra()
methods.
我无法检索传递给该方法的额外捆绑软件数据?
I can not retrieve the extra Bundle data that I am passing to the method?
startActivity()
/startActivityForResult()
的第三个参数是将选项传递给Android本身,而不是将数据传递给另一个活动.
That third parameter to startActivity()
/startActivityForResult()
are to pass options to Android itself, not to pass data to another activity.
我也尝试过... intent.putExtra("key",list);但也没有成功
I also tried with... intent.putExtra("key", list); but with no success either
通常,这可行.例如,此示例应用自Android 1.0开始就可以使用.启动器活动使用putExtra()
添加额外的字符串;其他活动使用getStringExtra()
来获取值.
In general, that works. For example, this sample app has worked since Android 1.0. The launcher activity uses putExtra()
to add a string extra; the other activity uses getStringExtra()
to get the value.
如果您在使用putExtra()
时仍然遇到问题,请发布一个单独的堆栈溢出问题,在其中提供用于设置和检索多余内容的代码,以及对症状的详细说明.
If you have continued problems using putExtra()
, post a separate Stack Overflow question, where you supply your code for setting and retrieving the extra, along with a detailed description of your symptoms.
这篇关于startActivityForResult(Intent intent,int requestCode,Bundle options)如何获取额外的bundle选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!