Several instance methods of intent transfer objects in Android series _android

Source: Internet
Author: User
Tags int size

In Android, there are 2 ways to transfer objects, namely, Bundle.putserializable (Key,object) and bundle.putparcelable (key, object) intent. 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, the following is an example I do for you
First we set up a project named: Objecttestdemo
Then we modify the Main.xml layout file, mainly add 2 buttons
View Plaincopy to Clipboardprint?

Copy Code 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 Jesson ' 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>

< 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 Jesson ' 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>
[Code]
Next we start to implement the project, respectively, establish Person.java implementation serializable interface, another Book.java implement Parcelable interface

[Code]
Package Com.test.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;

}

}

Copy Code code as follows:

Package Com.test.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;

}

}

Copy Code code as follows:

Package Com.test.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;

}

public void setpublishtime (int publishtime) {

This.publishtime = Publishtime;

}

public static final Parcelable.creator Creator = new Creator () {

Public book Createfromparcel (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);

}

}

Copy Code code as follows:

Package Com.test.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;

}

public void setpublishtime (int publishtime) {

This.publishtime = Publishtime;

}

public static final Parcelable.creator Creator = new Creator () {

Public book Createfromparcel (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);

}

}


Modify Objecttrandemo.java, and create two new activity, one is Objecttrandemo1.java, The other one is Objecttrandemo2.java. Used to display person pair image data, and book object data

Code

Copy Code code as follows:

Package Com.test.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 ();

}


public void Setupviews () {

Sbutton = (Button) Findviewbyid (R.id.button1);

Pbutton = (Button) Findviewbyid (R.id.button2);

Sbutton.setonclicklistener (this);

Pbutton.setonclicklistener (this);

}

Serializeable Method of passing objects

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 ();

}

}

}

Code

Copy Code code as follows:

Package Com.test.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 ();

}

public void Setupviews () {

Sbutton = (Button) Findviewbyid (R.id.button1);

Pbutton = (Button) Findviewbyid (R.id.button2);

Sbutton.setonclicklistener (this);

Pbutton.setonclicklistener (this);

}

Serializeable Method of passing objects

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 ();

}

}

}


Code
Copy Code code as follows:

Package Com.test.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 () + "" +

"You are:" + mperson.getage ());

Setcontentview (Mtextview);

}


Code
Copy Code code as follows:

Package Com.test.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 () + "" +

"You are:" + mperson.getage ());

Setcontentview (Mtextview);

}

}


Code
Copy Code code as follows:

Package Com.test.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 () + "" +

"Author is:" + mbook.getauthor () + "" +

"Publishtime is:" + mbook.getpublishtime ()); Setcontentview (Mtextview);

}

}

Copy Code code as follows:

Package Com.test.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 () + "" +

"Author is:" + mbook.getauthor () + "" +

"Publishtime is:" + mbook.getpublishtime ());

Setcontentview (Mtextview);

}

}

Here's the most important part: Modify the Androidmanifest.xml file (add two new ACTIVITY,OBJECTTESTDEMO1,OBJECTTESTDEMO2) and declare the code as follows (14th, 15 lines):

Code

Copy Code code as follows:

< XML version= "1.0" encoding= "Utf-8"?>

< manifest xmlns:android= "Http://schemas.android.com/apk/res/android"

Package= "Com.test.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= ". ObjecttestDemo1 "></activity>

< activity android:name= ". ObjecttestDemo2 "></activity>

</application>

< USES-SDK android:minsdkversion= "7"/>

</manifest>

< XML version= "1.0" encoding= "Utf-8"?>

< manifest xmlns:android= "Http://schemas.android.com/apk/res/android"

Package= "Com.test.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= ". ObjecttestDemo1 "></activity>

< activity android:name= ". ObjecttestDemo2 "></activity>

</application>

< USES-SDK android:minsdkversion= "7"/>

</manifest>

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.