The two methods of the Android intent delivery object (serializable,parcelable) are described in detail _android

Source: Internet
Author: User
Tags int size

The two methods of the Android intent delivery object (serializable,parcelable) are described in detail

Today I want to tell you about how to pass objects in Android intent, as far as I know there are two ways, 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 that 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; 
  } 
   

   

Book.java:

Package Com.tutor.objecttran; 
Import Android.os.Parcel; 
Import android.os.Parcelable; 
  public class book implements parcelable {private String bookname; 
  Private String author; 
   
  private int publishtime; 
  Public String Getbookname () {return bookname; 
  } public void Setbookname (String bookname) {this.bookname = BookName; 
  Public String Getauthor () {return author; 
  } public void Setauthor (String author) {this.author = author; 
  public int Getpublishtime () {return publishtime; 
  The public void setpublishtime (int publishtime) {this.publishtime = Publishtime; public static final parcelable.creator<book> Creator = new creator<book> () {public book Createf 
      Romparcel (Parcel source) {Book Mbook = new book (); 
      Mbook.bookname = Source.readstring (); 
      Mbook.author = Source.readstring (); 
      Mbook.publishtime = Source.readint (); 
    return mbook; } Public Book[] NewArray (int size) {return new book[size]; 
   
  } 
  }; 
  public int describecontents () {return 0; 
    public void Writetoparcel (Parcel Parcel, int flags) {parcel.writestring (bookname); 
    Parcel.writestring (author); 
  Parcel.writeint (Publishtime);  } 
}

Fourth step: Modify Objecttrandemo.java, and create two new activity, one is Objecttrandemo1.java, The other one is Objecttrandemo2.java. Used to display the person pair image data, and book Object data:, the code is as follows:

Objecttrandemo.java:

Package Com.tutor.objecttran; 
Import android.app.Activity; 
Import android.content.Intent; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.widget.Button; 
  public class Objecttrandemo extends activity implements Onclicklistener {private Button Sbutton,pbutton; 
  Public final static String Ser_key = "Com.tutor.objecttran.ser"; 
  Public final static String Par_key = "Com.tutor.objecttran.par"; 
    public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);   
    Setcontentview (R.layout.main); 
     
  Setupviews (); 
    //My usual style oh public void setupviews () {Sbutton = (Button) Findviewbyid (R.id.button1); 
    Pbutton = (Button) Findviewbyid (R.id.button2); 
    Sbutton.setonclicklistener (this); 
  Pbutton.setonclicklistener (this); 
    //serializeable the method of passing an object is public void Serializemethod () {Person Mperson = new person (); 
    Mperson.setname ("Frankie"); MPerson.setage (25); 
    Intent mintent = new Intent (this,objecttrandemo1.class); 
    Bundle mbundle = new Bundle (); 
    Mbundle.putserializable (Ser_key,mperson); 
     
    Mintent.putextras (Mbundle); 
  StartActivity (mintent); 
    //pacelable Pass object method public void Pacelablemethod () {Book Mbook = new book (); 
    Mbook.setbookname ("Android Tutor"); 
    Mbook.setauthor ("Frankie"); 
    Mbook.setpublishtime (2010); 
    Intent mintent = new Intent (this,objecttrandemo2.class); 
    Bundle mbundle = new Bundle (); 
    Mbundle.putparcelable (Par_key, Mbook); 
     
    Mintent.putextras (Mbundle); 
  StartActivity (mintent); 
    //Ammonium button Click event Response public void OnClick (View v) {if (v = = Sbutton) {Serializemethod (); 
    }else{Pacelablemethod ();  } 
  } 
}

Objecttrandemo1.java:

Package Com.tutor.objecttran; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.widget.TextView; 
public class ObjectTranDemo1 extends activity { 
  @Override public 
  void OnCreate (Bundle savedinstancestate) { 
    super.oncreate (savedinstancestate); 
     
    TextView Mtextview = new TextView (this); 
    Person Mperson = (person) getintent (). Getserializableextra (Objecttrandemo.ser_key); 
    Mtextview.settext ("Your name is:" + mperson.getname () + "/n" + "You are 
        :" + mperson.getage ()); 
     
    Setcontentview (Mtextview); 
  } 

Objecttrandemo2.java:

Package Com.tutor.objecttran; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.widget.TextView; 
public class ObjectTranDemo2 extends activity {public 
  
  void OnCreate (Bundle savedinstancestate) { 
    Super.oncreate (savedinstancestate); 
    TextView Mtextview = new TextView (this); 
    Book Mbook = (book) getintent (). Getparcelableextra (Objecttrandemo.par_key); 
    Mtextview.settext ("Book Name is:" + mbook.getbookname () + "n" + " 
             Author is:" + mbook.getauthor () + "n" + " 
             publi Shtime is: "+ mbook.getpublishtime ()); 
    Setcontentview (Mtextview); 
  } 

Fifth step: A more important step, modify the Androidmanifest.xml file (two new ACTIVITY,OBJECTTRANDEMO1,OBJECTTRANDEMO2) to declare the code as follows (14th, 15 lines):

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= 
"http://schemas.android.com/apk/res/" Android " 
   package=" Com.tutor.objecttran " 
   android:versioncode=" 1 " 
   android:versionname=" 1.0 "> 
  <application android:icon= "@drawable/icon" android:label= "@string/app_name" > 
    <activity android:name= ". Objecttrandemo " 
         android:label=" @string/app_name "> 
      <intent-filter> 
        <action android:name= "Android.intent.action.MAIN"/> 
        <category android:name= "Android.intent.category.LAUNCHER"/> 
      </intent-filter> 
    </activity> 
    <activity android:name= ". ObjectTranDemo1 "></activity> 
    <activity android:name=". ObjectTranDemo2 "></activity> 
  </application> 
  <uses-sdk android:minsdkversion=" 7 "/ > 
</manifest>  

The sixth step: Run the above project to view the effect chart:

Effect 1: First interface:

Effect 2: Click the Serializable button

Effect 3: Click the parcelable button:

Thank you for reading, I hope to help you, thank you for your support for this site!

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.