在 startActivity() 上传递 Bundle?

2023-12-03

将捆绑包传递到从当前活动启动的活动的正确方法是什么?共有财产?


您有几个选择:

1)使用Bundle来自Intent:

Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);  

2)创建一个新的包

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);

3)使用putExtra()Intent的快捷方法

Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);


然后,在启动的活动中,您可以通过以下方式阅读它们:

String value = getIntent().getExtras().getString(key)

NOTE:Bundle 具有适用于所有基本类型、Parcelables 和 Serializeds 的“get”和“put”方法。我只是将字符串用于演示目的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 startActivity() 上传递 Bundle? 的相关文章

随机推荐