方式1-简单数据:
参数传递
Intent intent =new Intent(MainActivity.this,MainActivity2.class);
intent.putExtra("name", "小明");
startActivity(intent);
参数获取
String parame1 = getIntent().getStringExtra("name");
方式2-bundle:
参数传递
Intent intent =new Intent(MainActivity.this,MainActivity2.class);
Bundle bundle=new Bundle();
bundle.putString("name", "小明");
bundle.putInt("age", 20);
intent.putExtras(bundle);startActivity(intent);
参数获取
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
int name = bundle.getInt("age");
方式3-对象:
参数传递
Intent intent =new Intent(MainActivity.this,MainActivity2.class);
User user = new User("小明",20);//User实现Serializable
intent.putExtra("user", user);
startActivity(intent);
参数获取
User parame1 = (User)getIntent().getSerializableExtra("user");
方式4-传递并获取返回的参数
传递参数
Intent intent =new Intent(MainActivity.this,MainActivity2.class);
intent.putExtra("name", "小明");
startActivityForResult(intent, 1);
处理返回参数
@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
super.onActivityResult(arg0, arg1, arg2);
//处理返回参数
}
返回参数
Intent intent =new Intent();
intent.putExtra("data", "你好");
setResult(1, intent);
finish();