在 startActivity() 上传递一个 Bundle? [英] Passing a Bundle on startActivity()?

查看:38
本文介绍了在 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆