-
基本类型参数传递
Activity间传递一些基本类型的数据可以直接进行传递,比如- 直接通过intent传递
//发送数据方
Intent intent = new Intent();
intent.putExtra("username",username);
intent.putExtra("userpass",userpass);
intent.setClass(ThisActivity.this,TargetActivity.class);
startActivity(intent);
//接受数据方
Intent intent = getIntent();
String name = intent.getStringExtra("username");
String pass = intent.getStringExtra("userpass);
* 通过Bundle传递
//传入
Intent it = new Intent(this,typeof(OtherActivity));
Bundle bd = new Bundle();
bd.PutBoolean("isNiubi",true);
bd.PutInt("Id",123456);
bd.PutString("name","詹姆斯");
it.PutExtras(bd);
StartActivity(it);
//接收
Bundle bd = Intent.Extras;
int Id = bd.GetInt("Id");
string Name = bd.GetString("name");
bool niubi = bd.GetBoolean("niubi");
- List对象传递
//传送
private List<String> myList;
Intent intent = new Intent();
intent.putExtra("myList", (Serializable) myList);
intent.setClass(A.this, B.class);
startActivity(intent); //开始跳转
// 接受
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras(); //获得全部数据
myList= (List<String>) bundle.getSerializable("myList");
- 一般对象传递
-
要传递的对象,实现Parcelable接口,Parcelable实现可以直接通过
Parcelable
插件实现。这样不用写代码
具体传递过程
-
//传送
Intent intent = new Intent();
intent.putExtra("myObj", myObj); //放入对象数据
intent.setClass(A.this, B.class);
startActivity(intent); //开始跳转
//接受
Intent intent = this.getIntent(); //获得当前的Intent
Bundle bundle = intent.getExtras(); //获得全部数据
myObj = bundle.getParcelable("myObj");