Using intent to deliver Java objects between activity in Android Engineering-implement Serializable 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 Serializable interface:

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

Package Com.ghj.vo;import Java.io.serializable;public class User implements serializable{private static final long Serialversionuid = -8278907591742219230l;private string Id;private string Name;public User (string ID, string name) {this. id = id;this.name = name;} Public 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;}}

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.putserializable ("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  = (user) Intent.getserializableextra ("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>

Note : The implementation of the serializable interface using the transitive object to implement the Java object delivery, then the class of the passed object must be serialized (such as the above user class implemented Serializable), Otherwise, an exception such as the following will appear: Java.lang.ClassCastException:com.ghj.vo.User cannot is cast to java.io.Serializable

"0-minute download demo"

Using intent to deliver Java objects between activity in Android Engineering-implement Serializable 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.