Intent与Bundle在activity之间传值的区别
Intent的putExtra()方法有:
public Intent putExtra(String name, boolean value);
public Intent putExtra(String name, byte value);
public Intent putExtra(String name, char value);
public Intent putExtra(String name, short value);
public Intent putExtra(String name, int value);
public Intent putExtra(String name, long value);
public Intent putExtra(String name, float value);
public Intent putExtra(String name, double value);
public Intent putExtra(String name, String value);
public Intent putExtra(String name, CharSequence value);
public Intent putExtra(String name, Parcelable value);
public Intent putExtra(String name, Parcelable[] value);
...
查看putExtra()方法中的源码,方法里面还是使用了Bundle对象,mExtras变量是在Intent类中定义的;Intent内部还是用bundle来实现数据传递的,只是在外面封装了一层。
public Intent putExtra(String name, String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}
Bundle类的作用
Bundle类用作携带数据,它类似于Map,用于存放key-value名值对形式的值。相对于Map,它提供了各种常用类型的putXxx()/getXxx()方法,如:putString()/getString()和putInt()/getInt(),putXxx()用于往Bundle对象放入数据,getXxx()方法用于从Bundle对象里获取数据。
Bundle的内部实际上是使用了HashMap
public void putBoolean(String key, boolean value);
public void putByte(String key, byte value);
public void putChar(String key, char value);
...
Intent在传值时没有Bundle的情况:
发送数据的Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edit);
button = (Button) findViewById(R.id.button1);
String txt = editText.getText().toString();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String string = editText.getText().toString();
Intent intent = new Intent(MainActivity.this, main.class);
intent.putExtra("name", string);
startActivity(intent);
}
});
}
接收数据的Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.txt2);
Intent intent = getIntent();
String string = intent.getStringExtra("name");
textView.setText(string);
}
使用Bundle 进行传值:
发送数据的Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edit);
button = (Button) findViewById(R.id.button1);
String txt = editText.getText().toString();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String string = editText.getText().toString();
Intent intent = new Intent(MainActivity.this, main.class);
Bundle bundle = new Bundle();
bundle.putString("name", string);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
接收方的Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.txt2);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String string = bundle.getString("name");
textView.setText(string);
}
Bundle在存取数据是比较灵活的,而Intent在存取数据的种类很少且没有指定数据类型;
想对数据进行比较灵活的操作如批量操作的话就用bundle;
Bundle是可以对对象进行操作的,而Intent不可以。Bundle相对于Intent比较偏下层,比Intent接口更多,更灵活,但Bundle仍需要借助Intent才能在Activity之间传递。
概括一下,Intent旨在数据传递,bundle旨在存取数据,当然intent也提供一部分数据的存取,但比起bundle就显得很不灵活。