<span id="Label3"></p><p><p><span style="font-family:Comic Sans MS; font-size:18px">In many cases, we need to pass objects between the activity, such as when you click on the item of a list and need to pass it to the next activity, what do we need to do?</span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px"><br></span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px">Android supports two ways of passing objects, one is bundle.putserializable, and the other is Bundle.putparcelable.</span></p></p><p><p><span style="font-family:‘Comic Sans MS‘"><span style="font-size:18px"><br></span></span></p></p><p><p><span style="font-family:‘Comic Sans MS‘"><span style="font-size:18px">So let's use an example to practice the object of activity:</span></span></p></p><p><p><span style="font-family:‘Comic Sans MS‘"><span style="font-size:18px"><br></span></span></p></p><p><p><span style="font-family:‘Comic Sans MS‘"><span style="font-size:18px">1. First establish two classes, one teacher class to represent the teacher, and one student class to represent the Students. The contents were as Follows:</span></span></p></p><p><p><span style="font-size:18px"><span style="font-family:‘Comic Sans MS‘"></span></span></p></p><pre name="code" class="java"><pre name="code" class="java"><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></pre></pre><p><p></p></p><p><p><span style="font-family:‘Comic Sans MS‘"><span style="font-size:18px"><br></span></span></p></p><pre name="code" class="java"><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></pre><span style="font-size:18px"><span style="font-size:18px"><br><br></span></span><p><p><span style="font-size:18px"><span style="font-family:Comic Sans MS">Where the teacher class implements the serializable interface, the student class implements the Parcelable Interface.</span></span></p></p><p><p><span style="font-size:18px"><span style="font-family:Comic Sans MS"><br></span></span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px">2. Create two activity, the first activity is called firstactivity, it has two objects: a teacher object, a student object, we now need to pass these two objects to the second activity, That is Secondactivity.</span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px"><br></span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px">First we look at the layout of the Firstactivity:</span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px"><br></span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px"><br></span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px">The contents of firstactivity are as Follows:</span></p></p><p><p> <span style="font-family:comic Sans MS; font-size:18px"> </span> </p> </p><pre name="code" class="java">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;}}</pre><p><p></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px"><br></span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px"><br></span></p></p><span style="font-family:Comic Sans MS; font-size:18px"><span style="font-family:Comic Sans MS; font-size:18px">then we'll look at the layout of the Secondactivity:</span></span><p><p><span style="font-family:Comic Sans MS; font-size:18px"><br></span><span style="font-family:Comic Sans MS; font-size:18px">There is only one textview.</span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px"><br></span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px">Then we take a look at the content of secondactivity, which mainly shows the objects passed to the Textview:</span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px"></span></p></p><pre name="code" class="java"><pre name="code" class="java">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);}}</pre></pre><br><span style="font-family:Comic Sans MS; font-size:18px"><span style="font-family:Comic Sans MS; font-size:18px">when we click on the "start passing object" button on the firstactivity interface, we jump to secondactivity with the following interface:</span></span><p><p></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px"><br></span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px"><br></span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px">obviously, both of these methods can successfully pass objects between the Activity.</span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px"><br></span></p></p><p><p><span style="font-family:Comic Sans MS; font-size:18px">Hope that the above content for your readers can bring help!</span></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Transfer objects between Android activity</p></p></span>
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