[英]Passing variables from Parent to Child views in Android
Whats the difference between pass the variables like this:像这样传递变量有什么区别:
Intent intent = new Intent(mCtx, DetailsActivity.class);
intent.putExtra("pId", id);
intent.putExtra("pType", type);
mCtx.startActivity(intent);
and using the keyword Bundle
?并使用关键字
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);
Im new to Android development and I am curious to which method is better or stadard when doing android development?我是 Android 开发的新手,我很好奇在进行 android 开发时哪种方法更好或标准?
Intent意图
public Intent putExtra(String name, String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}
Bundle捆
public void putString(String key, String value) {
unparcel();
mMap.put(key, value);
}
putExtras does not put your bundle inside Intent. putExtras 不会将您的包放入 Intent。 Instead, it copies it over to the current intent bundle.
相反,它将它复制到当前的意图包中。
public Intent putExtras(Bundle extras) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putAll(extras);
return this;
}
The first method is recommended.推荐第一种方法。 All samples are using the first putExtra (key, value) methods.
所有示例都使用第一个 putExtra(key, value) 方法。
In most scenarios you should know what you are passing to the next activity, so putExtra ( key, value ) makes sense because you know what the key is.在大多数情况下,您应该知道要传递给下一个活动的内容,因此 putExtra ( key, value ) 很有意义,因为您知道键是什么。
The only scenario I can think of to use the second method is a transient activity which receives bundle from previous activity and need to pass all information to next activity.我能想到的使用第二种方法的唯一场景是一个瞬态活动,它从前一个活动接收包并且需要将所有信息传递给下一个活动。 The transient activity does not need to know what is being passed and just pass everything it received to the next activity.
瞬态活动不需要知道正在传递什么,只需将它接收到的所有内容传递给下一个活动。
BTW, if you create your own Bundle, put the key values in and then call putExtra ( bundle), there is extra cost for the temporary bundle so it is less efficient.顺便说一句,如果您创建自己的 Bundle,将键值放入,然后调用 putExtra (bundle),临时 bundle 会产生额外成本,因此效率较低。
Whats the difference between pass the variables like this and using the keyword Bundle?
像这样传递变量和使用关键字 Bundle 有什么区别?
One puts the keys directly and one puts them in a Bundle first.一个是直接把钥匙放在一个包里,一个是先把它们放在一个 Bundle 中。 The end result is the same.
最终结果是一样的。
Im new to Android development and I am curious to which method is better or stadard when doing android development?
我是 Android 开发的新手,我很好奇在进行 android 开发时哪种方法更好或标准?
The first is "better" or more "standard" simply because it eliminates the need to create the bundle yourself first - it's a redundant step.第一个是“更好”或更“标准”,因为它消除了首先自己创建包的需要——这是一个多余的步骤。 You'd really only ever need to use that method if your code already had some logic that maintained a
Bundle
of data that you wanted to pass along in an Intent
as key / value pairs.如果您的代码已经有一些逻辑来维护您希望在
Intent
作为键/值对传递的数据Bundle
,那么您实际上只需要使用该方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.