Passing a Bundle on startActivity()?(在 startActivity() 上传递一个 Bundle?)
问题描述
将捆绑包传递给从当前活动启动的活动的正确方法是什么?共享属性?
What's the correct way to pass a bundle to the activity that is being launched from the current one? Shared properties?
推荐答案
你有几个选择:
1) 使用 Bundle 来自 意图:
1) Use the Bundle from the Intent:
Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);
2) 创建一个新的捆绑包
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)
注意: Bundle 对所有原始类型、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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 startActivity() 上传递一个 Bundle?
基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
