Using intent to deliver Java objects between activity in Android Engineering-Implement Parcelable interface

Source: Internet
Author: User

With intent, there are two ways to deliver Java objects between activity in an Android project: One passed object implements the serializable interface, and the other is the Parcelable interface that the passed object implements. This blog summarizes how to implement Java object delivery in the case of transitive objects implementing the Parcelable interface:

Code 1, add a file named "User.java":

Package Com.ghj.vo;import Android.os.parcel;import Android.os.parcelable;public class User implements parcelable{ private string Id;private string Name;public User (string ID, string name) {this.id = Id;this.name = name;} /** * Implements the Writetoparcel method of the Parcelable interface, writes the data that needs to be passed to the Parcel "container" to get the data from the Parcel "container".        */@Overridepublic void Writetoparcel (Parcel out, int flags) {out.writestring (ID); Out.writestring (name);} /** * Instantiate a static internal object Creator Implement Interface Parcelable.creator. Note: The public static final one cannot be less, the name of the internal object creator cannot be changed and must be capitalized. */public static final creator<user> Creator = new creator<user> () {@Override public user[] Newar        Ray (int size) {return new user[size]; /** * Reads the passed data value from the parcel "container" and encapsulates it into a single user entity */@Override Public user Createfromp        Arcel (Parcel in) {return new User (In.readstring (), in.readstring ()); }};p ublic String getId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GEtname () {return name;} public void SetName (String name) {this.name = name;} /** * Description content, generally return 0. */@Overridepublic int describecontents () {return 0;}}

Description: The user class above encapsulates the data that needs to be passed into a parcel object by implementing the Writetoparcel method of the Parcelable interface. The Createfromparcel method of implementing the Parcelable.creator interface through the internal implementation class takes the data from the parcel object and encapsulates it again into the user object. It is important to note that the order in which the passed data is written to parcel (see Implementing the Writetoparcel method) is consistent with the order in which the data is fetched from parcel (see Implementing the Createfromparcel method) .

Code 2, add a file named "Fromactivity.java":

Package Com.ghj.activity;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;import Com.ghj.vo.User; public class Fromactivity extends Activity implements Onclicklistener {private button button; @Overrideprotected void oncr Eate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.from); button = (button ) Findviewbyid (R.ID.OK_BTN); Button.setonclicklistener (this);} @Overridepublic void OnClick (view view) {Intent Intent  = new Intent (fromactivity.this, Toactivity.class); Bundle bundle = new bundle (); User user = New User ("56862586-108e-4749-93ca-440cd576ba92", "Xiang Wang");    Bundle.putparcelable ("user", user);    Intent.putextras (bundle);    StartActivity (intent);}}

Code 3, add a file named "Toactivity.java":

Package Com.ghj.activity;import Android.app.activity;import Android.content.intent;import android.os.Bundle;import Android.widget.textview;import Com.ghj.vo.user;public class Toactivity extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.to); TextView TextView = (TextView) Findviewbyid (r.id.show_user_info); Intent Intent = Getintent (); User User  = Intent.getparcelableextra ("user"), Textview.settext (User.getid () + ":" + user.getname ());}}

Code 4, add a file named "From.xml":

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <button        android:id= "@+id/ok_btn"        android:layout_width= "Match_parent"        android:layout_height= "wrap_content"        android:layout_centerhorizontal= "true"        android:layout_ Centervertical= "true"        android:layout_margin= "10DP"        android:text= "passing Java objects"/></relativelayout >

Code 5, add a file named "To.xml":

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <textview        android:id= "@+id/show_user_info"        android:layout_width= "wrap _content "        android:layout_height=" wrap_content "        android:layout_centerhorizontal=" true "        Android: Layout_centervertical= "true"/></relativelayout>
0 min Download demo

Using intent to deliver Java objects between activity in Android Engineering-Implement Parcelable interface

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.