Intent传递对象(序列化)
Parcelable
public class Person implements Parcelable { private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person [age=" + age + ", name=" + name + "]"; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } public static final Parcelable.Creator<Person> CREATOR=new Creator<Person>() { @Override public Person[] newArray(int size) { // TODO Auto-generated method stub return new Person[size]; } @Override public Person createFromParcel(Parcel source) { // TODO Auto-generated method stub Person person =new Person(); person.name=source.readString(); person.age=source.readInt(); return null; } }; @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(age); } public Person(int age, String name) { super(); this.age = age; this.name = name; } public Person() { super(); } }
Intent intent=new Intent(MainActivity.this,NextActivity.class);
Person person=new Person();
person.setName("zhang");
person.setAge(27);
intent.putExtra("person_data",person);
获取:
Person person=(Person)getIntent().getParcelableExtra("person_data");
生活本就不容易,当你生活容易时,肯定有人替你承担了你的那份不容易
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步