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.
I want to pass an ArrayList with data as an extra bundle option when I am calling the startActivityForResult method.
Sample of code:
ArrayList<String> list = new ArrayList<>();
list.add("test1");
list.add("test2");
Bundle bundleOptions = new Bundle();
bundleOptions.putStringArrayList("key", list);
startActivityForResult(intent, 10, bundleOptions);
Upon retrieving the data:
@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?
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?
I suspect you misunderstand how the result thing works.
Let's suppose you have a
HomeActivity
and aSettingsActivity
.HomeActivity
starts aSettingsActivity
with some parameters and wants to know some result. Here's how it works:HomeActivity
This is how you open the
SettingsActivity
:This is the call you receive when
SettingsActivity
is closed:SettingsActivity
This is just the necessary bit. Reads the input, builds the output and closes itself. I hope it's enough for a demonstration.
See this http://developer.android.com/training/basics/intents/result.html.
Correct.
If you wish to retrieve a value using
getExtras()
, useputExtras()
or individualputExtra()
methods.That third parameter to
startActivity()
/startActivityForResult()
are to pass options to Android itself, not to pass data to another activity.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 usesgetStringExtra()
to get the value.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.