像这样传递变量有什么区别:
Intent intent = new Intent(mCtx, DetailsActivity.class);
intent.putExtra("pId", id);
intent.putExtra("pType", type);
mCtx.startActivity(intent);并使用关键字Bundle
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
// Now let's Pass data using Bundle
Bundle bundle = new Bundle();
bundle.putString("pId", id);
bundle.putString("pType", type;
intent.putExtras(bundle);
startActivity(intent);我是Android开发的新手,我很好奇在进行android开发时,哪种方法更好或更标准?
发布于 2021-07-18 20:56:38
Intent
public Intent putExtra(String name, String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}捆绑包
public void putString(String key, String value) {
unparcel();
mMap.put(key, value);
}putExtras不会将您的捆绑包放在Intent中。相反,它会将其复制到当前intent包中。
public Intent putExtras(Bundle extras) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putAll(extras);
return this;
}发布于 2021-07-19 08:37:04
推荐使用第一种方法。所有示例都使用第一个putExtra (键,值)方法。
在大多数情况下,您应该知道要传递给下一个活动的是什么,所以putExtra (键,值)是有意义的,因为您知道键是什么。
我能想到的使用第二种方法的唯一场景是一个瞬态活动,它接收来自前一个活动的包,并需要将所有信息传递给下一个活动。瞬态活动不需要知道正在传递什么,只需将它收到的所有内容传递给下一个活动即可。
顺便说一句,如果你创建自己的包,把键值放在里面,然后调用putExtra (包),临时包会有额外的开销,所以效率较低。
发布于 2021-07-19 11:46:01
像这样传递变量和使用关键字
有什么不同?
一种是直接把钥匙放在一起,另一种是先把钥匙放在捆绑包里。最终结果是相同的。
我是安卓开发的新手,我很好奇做安卓开发时,哪种方法更好,哪种方法更标准?
https://stackoverflow.com/questions/68431855
复制相似问题