Intent传递基本类型相信大家都十分熟悉,如何传递Object对象或者ArrayList<Object>对象呢?
可以通过:
(1)public Intent putExtra (String name, Serializable value)
(2)public Intent putExtra (String name, Parcelable value)
public Intent putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)
一、通过实现Serializable接口传递
(1)首先让Person继承Serializable接口
- package com.example.hellojni;
- import java.io.Serializable;
- public class Person implements Serializable{
- /**
- * Auto generate
- */
- private static final long serialVersionUID = -5053412967314724078L;
- private String name;
- private int age;
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
- package com.example.hellojni;
- import java.io.Serializable;
- public class Person implements Serializable{
- /**
- * Auto generate
- */
- private static final long serialVersionUID = -5053412967314724078L;
- private String name;
- private int age;
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
(2)通过Intent来进行传输,具体方法是:public Intent putExtra (String name, Serializable value)
- Intent intent = new Intent(this, OtherActivity.class);
- intent.putExtra("person", new Person("bear", 22));
- startActivity(intent);
- Intent intent = new Intent(this, OtherActivity.class);
- intent.putExtra("person", new Person("bear", 22));
- startActivity(intent);
(3)另外一个Activity中打印值:
- Intent intent = getIntent();
- Person person = (Person)intent.getSerializableExtra("person");
- System.out.println("name:" + person.getName());
- System.out.println("age:" + person.getAge());
- Intent intent = getIntent();
- Person person = (Person)intent.getSerializableExtra("person");
- System.out.println("name:" + person.getName());
- System.out.println("age:" + person.getAge());
(4)传递ArrayList<Person>:
- Intent intent = new Intent(this, OtherActivity.class);
- ArrayList<Person> personList= new ArrayList<Person>();
- initList(personList); //初始化personList
- intent.putExtra("personList", personList);
- startActivity(intent);
- Intent intent = new Intent(this, OtherActivity.class);
- ArrayList<Person> personList= new ArrayList<Person>();
- initList(personList); //初始化personList
- intent.putExtra("personList", personList);
- startActivity(intent);
(注意传值类型必须是ArrayList而不能是List。调用的还是public Intent putExtra (String name, Serializable value)这个函数,另外一边获取的时候强制转化为ArrayList<Person>即可)
二、通过实现Parcelable接口传递
同上,Person类实现Parcelable接口即可:
传递Object用:public Intent putExtra (String name, Parcelable value)
传递ArrayList<Object>用:public Intent putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)
(同是序列化的接口,为神马Parcelable分开提供了2个函数,而传Serializable只提供了一个?这里我也很不解。。。)
转自:链接