1.首先我们创建我们的类:
我们创建了一个User类,其中的一个字段是userName,另一个是一个Bean,注意:我们在使用的时候,必须保证没一个Bean对象都要实现 Serializable,不然会出现错误。
public class User implements Serializable{
private String userName;
private Bean bean;
public static class Bean implements Serializable{
private String longId;
public String getLongId() {
return longId;
}
public void setLongId(String longId) {
this.longId = longId;
}
}
public User(String userName,Bean bean) {
this.userName = userName;
this.bean = bean;
}
public Bean getBean() {
return bean;
}
public void setBean(Bean bean) {
this.bean = bean;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
2.使用起来十分简单:
(1)放置数据:
User.Bean bean = new User.Bean();
bean.setLongId("666666666");
User user = new User("Tom",bean);
(2)在intent传递数据的时候:
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("user",user);
startActivity(intent);
(3)在接收的地方:
Intent intent = getIntent();
User user = (User) intent.getSerializableExtra("user");
这样操作下来,我们的User就被传递过来了。在不同进程间,可以使用这种方式传递数据。