Intent.putextra () Passing object objects or arraylist<object> (GO)

Source: Internet
Author: User

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
  1. Package com.example.hellojni;
  2. Import java.io.Serializable;
  3. Public class Person implements serializable{
  4. /** 
  5. * Auto Generate
  6. */
  7. private static final long serialversionuid = -5053412967314724078l;
  8. private String name;
  9. private int age;
  10. Public person (String name, int age) {
  11. this.name = name;
  12. this.age = age;
  13. }
  14. Public String GetName () {
  15. return name;
  16. }
  17. public void SetName (String name) {
  18. this.name = name;
  19. }
  20. public int getage () {
  21. return age;
  22. }
  23. public void Setage (int.) {
  24. this.age = age;
  25. }
  26. }
View Text Printing
  1. Package com.example.hellojni;
  2. Import java.io.Serializable;
  3. Public class Person implements serializable{
  4. /** 
  5. * Auto Generate
  6. */
  7. private static final long serialversionuid = -5053412967314724078l;
  8. private String name;
  9. private int age;
  10. Public person (String name, int age) {
  11. this.name = name;
  12. this.age = age;
  13. }
  14. Public String GetName () {
  15. return name;
  16. }
  17. public void SetName (String name) {
  18. this.name = name;
  19. }
  20. public int getage () {
  21. return age;
  22. }
  23. public void Setage (int.) {
  24. this.age = age;
  25. }
  26. }

(2) Transmission via Intent: Public Intent PutExtra (String name, Serializable value)

View Text Printing
    1. Intent Intent = new Intent (This, otheractivity.   Class);
    2. Intent.putextra ("Person", new Person ("Bear", 22));
    3. StartActivity (Intent);
View Text Printing
    1. Intent Intent = new Intent (This, otheractivity.   Class);
    2. Intent.putextra ("Person", new Person ("Bear", 22));
    3. StartActivity (Intent);

(3) Another activity in the print value:

View Text Printing
    1. Intent Intent = Getintent ();
    2. Person person = (person) Intent.getserializableextra ("person");
    3. System.  out.println ("Name:" + person.getname ());
    4. System.  out.println ("Age:" + person.getage ());
View Text Printing
    1. Intent Intent = Getintent ();
    2. Person person = (person) Intent.getserializableextra ("person");
    3. System.  out.println ("Name:" + person.getname ());
    4. System.  out.println ("Age:" + person.getage ());



(4) transmission Arraylist<person>:

View Text Printing
    1. Intent Intent = new Intent (This, otheractivity.   Class);
    2. arraylist<person> personlist= New arraylist<person> ();
    3. Initlist (personlist); //Initialize personlist
    4. Intent.putextra ("Personlist", personlist);
    5. StartActivity (Intent);
View Text Printing
    1. Intent Intent = new Intent (This, otheractivity.   Class);
    2. arraylist<person> personlist= New arraylist<person> ();
    3. Initlist (personlist); //Initialize personlist
    4. Intent.putextra ("Personlist", personlist);
    5. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.