【发布时间】:2013-02-18 12:45:54
【问题描述】:
我是 android 应用程序开发的新手,无法理解 bundle 究竟为我们做了什么。
谁能帮我解释一下?
【问题讨论】:
我是 android 应用程序开发的新手,无法理解 bundle 究竟为我们做了什么。
谁能帮我解释一下?
【问题讨论】:
我是 android 应用程序开发的新手,不明白什么 bundle 确实对我们有用。谁能帮我解释一下?
用我自己的话来说,你可以把它想象成一个 MAP,将 原始数据类型 和 对象 存储为一对 @987654324 @
Bundle 最常用于通过各种Activities 传递数据。提供 putType() 和 getType() 方法来存储和从中检索数据。
也可以Bundle作为onCreate()的参数,当你想在设备方向改变时保存数据时可以使用Activity的生命周期方法(在这种情况下Activity 被销毁并再次创建,非 null 参数为 Bundle)。
更多关于Bundle的方法你可以阅读reference at developer.android.com你应该从哪里开始,然后做一些演示应用程序来获得经验。
通过活动传递原始数据类型:
Intent i = new Intent(ActivityContext, TargetActivity.class);
Bundle dataMap = new Bundle();
dataMap.putString("key", "value");
dataMap.putInt("key", 1);
i.putExtras(dataMap);
startActivity(i);
通过活动传递值列表:
Bundle dataMap = new Bundle();
ArrayList<String> s = new ArrayList<String>();
s.add("Hello");
dataMap.putStringArrayList("key", s); // also Integer and CharSequence
i.putExtras(dataMap);
startActivity(i);
通过活动传递序列化对象:
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
private ArrayList<FooObject> foos;
public Foo(ArrayList<FooObject> foos) {
this.foos = foos;
}
public ArrayList<FooObject> getFoos() {
return this.foos;
}
}
public class FooObject implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
public FooObject(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
然后:
Bundle dataMap = new Bundle();
ArrayList<FooObject> foos = new ArrayList<FooObject>();
foos.add(new FooObject(1));
dataMap.putSerializable("key", new Foo(foos));
通过活动传递 Parcelable 对象:
还有更多代码,所以这里是如何做到这一点的文章:
有一种神奇的方法:getIntent(),它返回调用从那里启动 Activity 的 Intent(如果有任何数据也带有扩展数据)。
所以:
Bundle dataFromIntent = getIntent().getExtras();
if (dataFromIntent != null) {
String stringValue = dataFromIntent.getString("key");
int intValue = dataFromIntent.getInt("key");
Foo fooObject = (Foo) dataFromIntent.getSerializable("key");
// getSerializble returns Serializable so we need to cast to appropriate object.
ArrayList<String> stringArray = dataFromIntent.getStringArrayList("key");
}
使用Bundle作为onCreate()方法的参数:
您在 onSaveInstanceState() 方法中存储数据,如下所示:
@Override
public void onSaveInstanceState(Bundle map) {
map.putString("key", "value");
map.putInt("key", 1);
}
并在 onCreate() 方法中恢复它们(在这种情况下是 Bundle 作为参数不为空)如下:
@Override
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
String stringValue = savedInstanceState.getString("key");
int intValue = savedInstanceState.getString("key");
}
...
}
注意:onRestoreInstanceState()方法也可以恢复数据,但不常用(在onStart()方法和onCreate() 之前被调用)。
【讨论】:
用一般的英语:“它是一堆东西,或一定数量的材料,捆绑或包裹在一起。”
在 Android 中也是如此“它是键及其值的集合,用于在其中存储某种数据。”
【讨论】:
Bundle 通常用于在各个组件之间传递数据。可以通过 getExtras() 方法从 Intent 中检索的 Bundle 类。
您还可以通过 Intent 对象的重载 putExtra() 方法将数据直接添加到 Bundle。 Extras 是键/值对,键始终是字符串类型。作为值,您可以使用原始数据类型。
接收组件可以通过 Intent 对象上的 getAction() 和 getData() 方法访问这些信息。可以通过 getIntent() 方法检索此 Intent 对象。和 接收到意图的组件可以使用 getIntent().getExtras() 方法调用来获取额外的数据。
MainActivity
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString(“Key“, myValue);
intent.putExtras(bundle);
startActivity(intent);
第二活动
Bundle bundle = getIntent().getExtras();
String myValue= bundle.getString(“key“);
【讨论】:
捆绑或包裹在一起的一堆东西或一定数量的材料。它是字典的意思……同一个Bundle是数据的集合。数据可以是任何类型,即 String、int、float、boolean 和任何可序列化的数据。我们可以使用捆绑包Bundle 将一个 Activity 的数据共享和保存到另一个。
【讨论】:
将其视为一束数据,在将数据从一个 Activity 传递到另一个 Activity 时使用。
documentation 将其定义为
“从字符串值到各种 Parcelable 类型的映射。”
您可以将数据放入 Bundle 中,然后将该 Bundle 传递给多个活动。这很方便,因为您不需要传递单个数据。您将所有数据放入 Bundle 中,然后只需传递 Bundle,而不是单独发送数据。
【讨论】:
实际上是一堆东西;信息:你把东西放在那里(字符串、整数等),然后在使用意图时将它们作为单个参数(包)传递。
然后您的目标(活动)可以再次将它们取出并读取 ID、模式、设置等。
【讨论】:
从 String 值到各种 Parcelable 类型的映射。Click here
例子:
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.extras.putString(key, value);
mIntent.putExtras(mBundle);
将价值从一项活动发送到另一项活动。
【讨论】: