今天发现自己连Bundle类都没有搞清楚,于是花时间研究了一下。
根据google官方的文档(http://developer.android.com/reference/android/os/Bundle.html)
Bundle类是一个key-value对,“A mapping from String values to various Parcelable types.”
两个activity之间的通讯可以通过bundle类来实现:
开始做了一个demo来模拟activity互相传递。
Activity1
public class Activity1 extends Activity {
Button bt_1;
EditText et_1;
TextView tv_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pass1);
bt_1 = (Button) findViewById(R.id.bt_1);
et_1 = (EditText) findViewById(R.id.et_1);
tv_1 = (TextView) findViewById(R.id.tv_1);
try {
// 获取返回值
Bundle bundle = this.getIntent().getExtras();
// 获取数据内容
String userReturn = bundle.getString("userReturn");
// 显示在TextView中
tv_1.setText("传回:" + userReturn);
} catch (Exception e) {
System.out.println("error:" + e);
}
bt_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 创建Intent
Intent intent = new Intent();
// 升职from和to的activity
intent.setClass(Activity1.this, Activity2.class);
// 设置要传送的数据
Bundle bundle = new Bundle();
bundle.putString("userInput", et_1.getText().toString());
// 将数据设置到Intent中
intent.putExtras(bundle);
Activity1.this.startActivity(intent);
Activity1.this.finish();
}
});
}
}
public class Activity2 extends Activity {
Button bt_2;
TextView tv_2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pass2);
tv_2 = (TextView) findViewById(R.id.tv_2);
bt_2 = (Button) findViewById(R.id.bt_2);
Bundle bundle = this.getIntent().getExtras();
// 获取数据
final String userInput = bundle.getString("userInput");
// 显示在TextView中
tv_2.setText(userInput);
bt_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 创建Intent实体
Intent intent = new Intent();
intent.setClass(Activity2.this, Activity1.class);
// 创建要传回的数据
Bundle bundle = new Bundle();
bundle.putString("userReturn", "return value " + userInput);
intent.putExtras(bundle);
Activity2.this.startActivity(intent);
Activity2.this.finish();
}
});
}
}
这个demo可以完成两个Activity之间的互相调用,但是无法说明是谁传递过来的数据,有可能是别的activity传递过来的数据。
所以上网查了下相关文档,添加了一下内容,主要用的是startActivityForResult来实现
Activity1中修改
<pre name="code" class="java" style="color: rgb(51, 51, 51); font-size: 14px; line-height: 26px;"><pre name="code" class="java"> //Activity1.this.startActivity(intent);
Activity1.this.startActivityForResult(intent, 123456);
//Activity1.this.finish();
Activity2中修改
<pre name="code" class="java"> //Activity2.this.startActivity(intent);
Activity2.this.setResult(123456, intent);
Activity2.this.finish();
之后需要在Activity1中重写 onActivityResult方法替代原有的try catch
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 123456){
Bundle bundle = data.getExtras();
String userReturn = bundle.getString("userReturn");
tv_1 = (TextView) findViewById(R.id.tv_1);
tv_1.setText("传回:"+userReturn);
}
}