方法1: 1给2传值
Activity1:
Java代码
Intent intent = new Intent(mCtx,Activity1.class) ;
//将当前用户传递下去
intent.putExtra("user", mUser);
startActivity(intent);
里边mUser是一个对象,"user"是对象mUser的key
Activity2:
Java代码
mUser = (User) getIntent().getSerializableExtra("user");
说明:mUser是对象,getIntent()是Activity的方法,值得注意的是得到的mUser 不是引用,而是new了一个对象并赋值,如果这东西要回传的话,要startActivity(intent);就没用了。、
所以用方法2:
Activity1:
Java代码、
intent.setClass(mCtx, Activity2.class);
intent.putExtra("user", mUser);
startActivityForResult(intent, 1);
并且在Activity1中
Java代码
protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==1 || data.getSerializableExtra("putUserbyhua")!=null)
mUser=(User) getIntent().getSerializableExtra("putUserbyhua");
}
Activity2:
获取传来的值
Java代码
mIntent = getIntent() ;
mUser=(User) mIntent.getSerializableExtra(UserConst.USER_DATA);
在按返回键时跳转到1
Java代码
@Override
publicboolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
final Intent intent=new Intent();
intent.setClass(mCtx, Activity2.class);
intent.putExtra("putUserbyhua",mUser);
this.setResult(1);
this.finish();
}
return super.onKeyDown(keyCode, event);
}