我是新手安卓应用开发者,不理解bundle实际上为我们做了什么。能有人为我解释一下吗?
我的理解是,你可以将Bundle想象成一个MAP
,它存储着原始数据类型和对象作为键值对
。
Bundle
通常用于通过各种Activities
传递数据。提供putType()
和getType()
方法用于从中存储和检索数据。
此外,在Activity的生命周期方法onCreate()
的参数中使用Bundle
时,当设备方向改变时,可以用于保存数据(在这种情况下,Activity被销毁并再次创建,其参数不为空)。
您可以在开发者文档中的参考页面了解更多关于Bundle
及其方法的信息,然后制作一些演示应用程序以获得经验。
通过Activities传递原始数据类型:
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));
通过 Activities 传递 Parcelable 对象:
这里有很多的代码,以下是如何实现的文章:
有一个神奇的方法:getIntent()
,它返回启动该方法的 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");
}
onCreate()
方法中使用Bundle作为参数的用法:
您可以像下面这样在onSaveInstanceState()
方法中存储数据:
@Override
public void onSaveInstanceState(Bundle map) {
map.putString("key", "value");
map.putInt("key", 1);
}
接下来在 onCreate()
方法中恢复它们(在这种情况下,将Bundle
作为参数不为null):
@Override
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
String stringValue = savedInstanceState.getString("key");
int intValue = savedInstanceState.getString("key");
}
...
}
注意:你也可以在onRestoreInstanceState()
方法中还原数据,但这不常见(它在onStart()
方法之后调用,而onCreate()
在其之前调用)。
通常的英文定义是:“这是一组事物,或者是材料的数量,用绳子或包裹捆绑在一起。”
在Android中,"它是一个键及其值的集合,用于存储某些类型的数据。"
Bundle通常用于在不同组件之间传递数据。可以通过从意图(intent)中使用getExtras()方法检索的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);
SecondActivity
Bundle bundle = getIntent().getExtras();
String myValue= bundle.getString(“key“);
然后,您的目标(活动)可以再次获取它们并读取ID,模式,设置等。
一个将字符串值映射到各种可 Parcelable 类型的映射表。点击这里
示例:
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.extras.putString(key, value);
mIntent.putExtras(mBundle);