startActivityForResult(Intent intent, int requestCode, Bundle options) how to retrieve the extra bundle option?
I am sure that I am missing something
I suspect you misunderstand how the result thing works.
Let's suppose you have a HomeActivity
and a SettingsActivity
. HomeActivity
starts a SettingsActivity
with some parameters and wants to know some result. Here's how it works:
HomeActivity
This is how you open the SettingsActivity
:
public void openSettings() {
Intent i = new Intent(this, SettingsActivity.class);
i.putExtra("myParam", 1);
startActivityForResult(i, 10);
}
This is the call you receive when SettingsActivity
is closed:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 10) {
if (resultCode == RESULT_OK) {
// Get result from the result intent.
String result = data.getStringExtra("myResult");
// Do something with result...
}
}
}
SettingsActivity
This is just the necessary bit. Reads the input, builds the output and closes itself. I hope it's enough for a demonstration.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Retrieve the parameter.
int param = getIntent().getIntExtra("myParam");
// Get a result somewhere.
String resultValue = "RESULT=" + param;
// Build a result intent and post it back.
Intent resultIntent = new Intent();
resultIntent.putExtra("myResult", resultValue);
setResult(RESULT_OK, resultIntent);
finish();
}
See this http://developer.android.com/training/basics/intents/result.html.
Bundle extras does not contain the extra bundle that I am trying to pass.
Correct.
What I am missing
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?
That third parameter to startActivity()
/startActivityForResult()
are to pass options to Android itself, not to pass data to another activity.
I also tried with... intent.putExtra("key", list); but with no success either
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.
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.