可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
Thank you for your time and effort.
回答1:
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.
回答2:
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.