Transfer objects between Android activity

Source: Internet
Author: User

In a very long time, we need to pass objects between the activity, for example, when you click on the item of a list and need to pass it to the next activity, what do we need to do?


Android supports two ways to pass objects. One is bundle.putserializable way, one is bundle.putparcelable.


So let's use a sample to practice the object of activity:


1. First establish two classes, one teacher class to represent the teacher, and one student class to represent the students. The contents are as follows:

<span style= "FONT-SIZE:18PX;" >public class Teacher implements Serializable {private static final long Serialversionuid = -7060210544600464481l;priv  Ate string name;private int age;private string addr;public Teacher (string name, int age, string addr) {super (); this.name = Name;this.age = Age;this.addr = addr;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public String getaddr () {return addr;} public void setaddr (String addr) {this.addr = addr;} @Overridepublic string toString () {String temp = "Teacher Name:" + THIS.name + "Age:" + this.age + "Address:" + This.addr;return temp;} }</span>


<span style= "FONT-SIZE:18PX;" >import Android.os.parcel;import Android.os.parcelable;public class Student implements parcelable {private String name;private int age;private String addr;public static final parcelable.creator<student> Creator = new Creator<s Tudent> () {@Overridepublic student[] newarray (int size) {return new student[size];} @Overridepublic Student Createfromparcel (Parcel source) {Student Stu = new Student (); stu.name = Source.readstring (); Stu.age = Source.readint (); stu.addr = Source.readstring (); return Stu;}}; @Overridepublic int describecontents () {//TODO auto-generated method Stubreturn 0;} @Overridepublic void Writetoparcel (Parcel dest, int flags) {dest.writestring (name);d Est.writeint (age); Dest.writestring (addr);} Public Student () {}public-Student (string name, int age, string addr) {super (); this.name = Name;this.age = Age;this.addr = A DDR;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return aGE;} public void Setage (int.) {this.age = age;} Public String getaddr () {return addr;} public void setaddr (String addr) {this.addr = addr;} @Overridepublic string toString () {String temp = "Student Name:" + THIS.name + "Age:" + this.age + "Address:" + This.addr;return temp;} }</span>


The teacher class implements the serializable interface, and the student class implements the Parcelable interface.


2. Create two activity, the first activity is called firstactivity. There are two objects in it: A Teacher object, a student object, and we now need to pass these two objects to the second activity. That is secondactivity.


First we look at the layout of the firstactivity:



The contents of the firstactivity are as follows:

Import Android.os.bundle;import android.app.activity;import Android.content.intent;import Android.view.Menu;import Android.view.view;import Android.view.view.onclicklistener;public class Firstactivity extends Activity implements Onclicklistener{public static final String teacher_key = "Key_teacher";p ublic static final String student_key = "Key_stude NT ";p rivate Teacher Teacher = new Teacher (" Liu Bei ", 36," Jingzhou ");p rivate Student Student = new Student (" Zhang Fei ", 32," Xinye "); @Overridep rotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_first); Findviewbyid (R.id.button1). Setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.button1:bundle bundle = new Bundle (); bundle.putserial Izable (Teacher_key, TEACHER); Bundle.putparcelable (Student_key, STUDENT); Intent Intent = new Intent ( Firstactivity.this,secondactivity.class); Intent.putextras (bundle); startactivity (intent); break;default:break;}}



then we'll look at the layout of the secondactivity:


There is only one textview.


Then we take a look at the content of Secondactivity, which is mainly to show the objects passed over to TextView which:

Import Android.os.bundle;import android.app.activity;import Android.content.intent;import Android.view.Menu;import Android.widget.textview;public class Secondactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_second); Intent Intent = This.getintent (); Bundle bundle = Intent.getextras (); Teacher t = (Teacher) bundle.getserializable (Firstactivity.teacher_key); Student s = bundle.getparcelable (Firstactivity.student_key); String text = t.tostring () + "\ n" + s.tostring ();((TextView) Findviewbyid (R.id.text_view)). SetText (text);}}

when we click on the "Start passing Object" button on the Firstactivity interface, we jump to secondactivity and the interface is as follows:



Obviously, both of these methods can successfully pass objects between the activity.


Hope that the above content for your readers can bring help!

Transfer objects between Android activity

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.