Android Intent uses serializable and parcelable to pass objects

Source: Internet
Author: User

How to pass objects in intent in Android, as far as I know there are two methods, one is bundle.putserializable (Key,object), the other is bundle.putparcelable (Key, Object) Of course, these object is a certain condition, the former is the implementation of the serializable interface, and the latter is the implementation of the Parcelable interface, in order to make it easier for everyone to understand I still write a simple demo, everyone step by step with me!

The first step: new Android project named Objecttrandemo (more than a class!) The directory structure is shown below:

Step Two: Modify the Main.xml layout file (I added two buttons here) code as follows

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=  
"http://schemas.android.com/apk/" Res/android "
    android:orientation=" vertical "
    android:layout_width=" fill_parent "
    android:layout_" height= "Fill_parent"
    >  
<textview
    android:layout_width= "fill_parent"
    android:layout_ height= "Wrap_content"
    android:text= "Welcome to Mr Wei ' s blog."
    />  
<button
    android:id= "@+id/button1"
    android:layout_width= "Fill_parent"
    android: layout_height= "Wrap_content"
    android:text= "Serializable"/> <button android:id=  
"@+id
    " /button2 "
    android:layout_width=" fill_parent "
    android:layout_height=" wrap_content "
    android:text=" "Parcelable"
/>  
</LinearLayout>

Step three: Create two new classes one is Person.java implement Serializable interface, another Book.java implement Parcelable interface, the code is as follows:

Person.java:

Package Com.tutor.objecttran;  
Import java.io.Serializable;  
public class Person implements Serializable {  
    private static final long Serialversionuid = -7060210544600464481l;
  
   private String name;  
    private int 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;  
    }  
          
}
  

Related Article

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.