Intent object Passing (parcelable passing objects and collections of objects)

Source: Internet
Author: User

next , we'll discuss how to use Parcelable to implement an object transfer between intent

I. Implementation of Object passing

First create the User.java implementation Parcelable interface:

package org.yayun.demo;import java.io.serializable;import android. R.integer;import Android.os.parcel;import Android.os.parcelable;public class User implements parcelable {private string Name;private int level;public string getName () {return name;} public void SetName (String name) {this.name = name;} public int Getlevel () {return level;} public void SetLevel (Int. level) {this.level = level;} Public User (String name, int. level) {this.name = Name;this.level = level;} Public User () {//constructor method}public int describecontents () {//overwrite method return 0;} public void Writetoparcel (Parcel Parcel, int flags) {//Overwrite method parcel.writestring (name);p arcel.writeint (level);} public static final parcelable.creator<user> Creator = new creator<user> () {//methods must be implemented public User Createfromparcel (Parcel source) {User user = new User (); user.name = Source.readstring (); user.level = Source.readint (); return user;} Public user[] NewArray (int size) {return new user[size];}}; 

Parcelable.creator must be implemented, otherwise it will be an error:

Here's a look at two activity

1.mainactivity.java

Package Org.yayun.demo;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 MainActivity Extends Activity {private Button button;public void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Life cycle Method Super.setcontentview (R.layout.main); Sets the layout manager to use button = (Button) Findviewbyid (R.ID.BTN); Button.setonclicklistener (new Onclicklistener () {public void  OnClick (View v) {User user1 = new User ("Yayun", 7);//Instantiate object Intent Intent = new intent (); Intent.setclass (Mainactivity.this, Loginactivity.class); Bundle bundle = new Bundle (), bundle.putparcelable ("user", User1),//serialization Intent.putextras (Bundle),//Send data startactivity ( intent);//Start Intent}});}}

2.loginactivity.java:

Package Org.yayun.demo;import Android.app.activity;import Android.content.intent;import android.os.Bundle;import Android.widget.textview;public class Loginactivity extends Activity {private TextView TextView; User user;public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);// Life cycle Method Super.setcontentview (R.layout.login); Set the layout manager to use TextView = (TextView) Findviewbyid (r.id.text); Intent Intent = This.getintent (); user = (user) Intent.getparcelableextra ("user"); Textview.settext ("Username:" + user.getname () + "User Level" + string.valueof (User.getlevel () ));}}


3. Run the following example:

You can see and implement the same results as the Serialiable interface.



Second, the realization of object collection delivery

Here we only need to change two activity

1.mainactivity.java:

Package Org.yayun.demo;import Java.util.arraylist;import Java.util.list;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 Mainactivity extends Activity {private button button; List<user> list=new arraylist<user> ();p ublic void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Life cycle Method Super.setcontentview (R.layout.main); Sets the layout manager to use button = (Button) Findviewbyid (R.ID.BTN); Button.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {User User1 = new User ("Yayun", 7);//Instantiate the object user User2=new user ("Feifei", 9); List.add (user1); List.add ( User2); Intent Intent = new Intent (); Intent.setclass (Mainactivity.this, Loginactivity.class);                  Bundle bundle = new Bundle (), Bundle.putparcelablearraylist ("list", (arraylist<user>) list);//serialization, to be aware of Intent.putextras (bundle);//Send Data startactivity (intent);// Start Intent}});}} 


2.loginactivity.java

Package Org.yayun.demo;import Java.util.list;import Android.app.activity;import android.content.intent;import Android.os.bundle;import Android.widget.textview;public class Loginactivity extends Activity {private TextView TextView; List<user> list; User user2;public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);// Life cycle Method Super.setcontentview (R.layout.login); Set the layout manager to use TextView = (TextView) Findviewbyid (r.id.text); Intent Intent = This.getintent (); list =  Intent.getparcelablearraylistextra ("list");//getparcelablearraylistextra method User2= (User) list.get (1); Textview.settext ("Username:" + user2.getname () + "User Level" + string.valueof (User2.getlevel ());}}


3. Running the example:

Run successfully!

Finally, note configuring Loginactivity in the Androidmanifest.xml file

Summarize

1. Realize parcelable interface to cover several methods and implement Parcelable.creator method;

2.bundle. putparcelable ("User", User1);//serialization

3.user = (user) intent. Getparcelableextra ("user");//Get Object

4.bundle. putparcelablearraylist ("List", (arraylist<user>) list);//Deposit into List Object array

5.list = Intent. Getparcelablearraylistextra ("list");//getparcelablearraylistextra method gets an array of objects

6. The system seems to provide only a list object array of delivery methods, how to pass the other collections? I hope Daniel will inform you.

Like a friend to order a praise Bai! Pay attention to the chant! Thank you

Intent object Passing (parcelable passing objects and collections of objects)

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.