在 startActivity() 上传递 Bundle? [英] Passing a Bundle on startActivity()?
问题描述
将包传递给从当前启动的 Activity 的正确方法是什么?共享属性?
What's the correct way to pass a bundle to the activity that is being launched from the current one? Shared properties?
推荐答案
您有几个选择:
1) 使用 Bundlehttp://developer.android.com/reference/android/content/Intent.html" rel="noreferrer">意图:
1) Use the Bundle from the Intent:
Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);
2) 创建一个新的 Bundle
2) Create a new Bundle
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);
3) 使用 putExtra() Intent 的快捷方法
3) Use the putExtra() shortcut method of the Intent
Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);
然后,在启动的 Activity 中,您可以通过以下方式阅读它们:
Then, in the launched Activity, you would read them via:
String value = getIntent().getExtras().getString(key)
注意: 捆绑包具有适用于所有原始类型、Parcelable 和 Serializable 的get"和put"方法.我只是出于演示目的使用字符串.
NOTE: Bundles have "get" and "put" methods for all the primitive types, Parcelables, and Serializables. I just used Strings for demonstrational purposes.
这篇关于在 startActivity() 上传递 Bundle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!