Activity间传递数据

 1.从当前的Activity传递数据到下一个Activity:

(1)发送方(当前的Activity):

Bundle bundle = new Bundle();
bundle.putString("name", "Haoye");
bundle.putInt("id", 10); Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
intent.putExtras(bundle); startActivity(intent);

(2)接收方(即将跳转到的Activity):

//---在onCreate函数里添加:
Bundle bundle = this.getIntent().getExtras();
String name = bundle.getString(“name");
int id = bundle.getInt("id");

2.从当前Activity返回数据到前一个Activity:

使用startActivityForResult()函数跳转;

接收方:

在原来的Activity(即MainActivity)中重写 onActivityResult() 函数:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case RESULT_OK:
Bundle bundle = data.getExtras();
//...
break;
default:
break;
}
}

发送方:

在SecondActivity返回前调用setResult()函数:

Bundle bundle = new Bundle();
bundle.putString("name", "Haoye");
bundle.putInt("id", 10); Intent intent = new Intent();
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
上一篇:bootstrap菜单完美解决---原创


下一篇:CentOS配置FTP(VSFTPD)