Passing Objects Using Intent

Use Serializable to pass objects, the code is simple and inefficient

Using parcelable to pass objects, the code is complex and efficient (about 10 times)

 

 

Implementation steps

1. The class should be serialized (serialization is to convert the object into a storable state)

     implements  Serializable

public class Person  implements Serializable{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

 

2. New a Person object, put data, and pass it through intent (only objects of implements Serializable can be passed through intent)

  

    // pass the object

Person person =new Person();
person.setName("abc");
Intent intent=new Intent(this,TargetActivity.class);
intent.putExtra("object",person);
startActivity(intent);

  

    //Receive object

    

Person person=(Person)getIntent().getSerializableExtra("abc");

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326219007&siteId=291194637