Intent passing Basic Types I believe everyone is very familiar with how to pass object objects or arraylist<object> objects ?
You can do this by:
(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)
First, through the implementation of the Serializable interface transfer
(1) First let person inherit serializable interface
View Text Printing
- 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.) {
- this.age = age;
- }
- }
View Text Printing
- 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.) {
- this.age = age;
- }
- }
(2) Transmission via Intent: Public Intent PutExtra (String name, Serializable value)
View Text Printing
- Intent Intent = new Intent (This, otheractivity. Class);
- Intent.putextra ("Person", new Person ("Bear", 22));
- StartActivity (Intent);
View Text Printing
- Intent Intent = new Intent (This, otheractivity. Class);
- Intent.putextra ("Person", new Person ("Bear", 22));
- StartActivity (Intent);
(3) Another activity in the print value:
View Text Printing
- Intent Intent = Getintent ();
- Person person = (person) Intent.getserializableextra ("person");
- System. out.println ("Name:" + person.getname ());
- System. out.println ("Age:" + person.getage ());
View Text Printing
- Intent Intent = Getintent ();
- Person person = (person) Intent.getserializableextra ("person");
- System. out.println ("Name:" + person.getname ());
- System. out.println ("Age:" + person.getage ());
(4) transmission Arraylist<person>:
View Text Printing
- Intent Intent = new Intent (This, otheractivity. Class);
- arraylist<person> personlist= New arraylist<person> ();
- Initlist (personlist); //Initialize personlist
- Intent.putextra ("Personlist", personlist);
- StartActivity (Intent);
View Text Printing
- Intent Intent = new Intent (This, otheractivity. Class);
- arraylist<person> personlist= New arraylist<person> ();
- Initlist (personlist); //Initialize personlist
- Intent.putextra ("Personlist", personlist);
- StartActivity (Intent);
(Note that the value type must be ArrayList and not a list.) Call or public Intent PutExtra (String name, Serializable value) This function, the other side of the acquisition of the time forced to convert to arraylist<person> can be)
Second, through the implementation of Parcelable interface transfer
Ditto, the person class implements the Parcelable interface:
Pass Object by: Public Intent PutExtra (String name, parcelable value)
Pass arraylist<object> : Public Intent Putparcelablearraylistextra (String name, arraylist<? extends Parcelable> value)
(The same is the serialized interface, for God horse parcelable separate provides 2 functions, and serializable only provide one? Here I am also very puzzled ... )
Transfer from: Link
Intent.putextra () Passing object objects or arraylist<object> (GO)