Android Learning Path--android Four components activity (ii) data transfer

Source: Internet
Author: User

The previous article on the creation and activation of activity, this one, we talk about the activity of data transmission

Data transfer between activity, the main introduction here is the transfer of simple data between activity, directly with the bundle to pass the basic data type data. Another data type is parcelable and serialable

There are two ways to pass data with bundles, and this article explains it from two different aspects.

First, the use of bundles to pass the basic data types

1. Transmit data at start-up, use intent put method, write data to bundle, then startactivity (intent) will be able to transfer data to target activity

Passing Data in aactivity:

Intent Intent = new Intent (aactivity.this, Bactivity.class); Intent.putextra ("Name", "Android"); Intent.putextra ("Age" , Intent.putextra ("Isstudent", true); startactivity (intent);
Receive data in Bactivity: The target activity obtains the intent object through the Getintent () method, and then it can get the data of the basic data type passed by GetString getInt Getboolean.

String name = Getintent (). Getstringextra ("name"); int = Getintent (). Getintextra ("age", 0); Boolean isstudent = Getintent (). Getbooleanextra ("Isstudent", false);

Execution Result:




2. Return data when closed

Start activity in Aactivity

Intent Intent = new Intent (aactivity.this, Bactivity.class); Startactivityforresult (Intent, 1);
Close activity in bactivity and return data to Aactivity

The work to be done in B is to put the data that will be returned into intent, then set the return result code Setresult (ResultCode) General result code is ACTIVITY.RESULT_OK, then call the Finish method, close the activity, Return to previous activity

Intent Intent = new Intent () Intent.putextra ("name", "This is the data passed by bactivity off"); Setresult (activity.result_ok,intent); Finish ();

The next step is to receive the returned data in the activity before the start, overriding the Activityresult method, and the third parameter is the intent with the data.

@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO auto-generated method stub Super.onactivityresult (Requestcode, ResultCode, data), if (ResultCode = = ACTIVITY.RESULT_OK) {switch (Requestcode) { Case 100:resulttxt.settext (Data.getstringextra ("result")); break;default:break;}}}

Ii. transfer of complex data types using Parcelabel and serializable

1, parcelable transfer data

Assuming that you want to pass the data type you define, implement the Parcelable interface for your own defined data type. Delivery methods such as the following:

Define the data type yourself to implement the Parcelable interface, then implement the two methods Describecontents Writetoparcel and create a creator, such as the following:

Package Com.yang.intentdemo;import Java.io.serializable;import Android.os.parcel;import android.os.Parcelable; public class Person implements parcelable {private string name;private int age;private string job;public person (string nam e, int age, String job) {super (); this.name = Name;this.age = Age;this.job = Job;} @Overridepublic String toString () {return "person [name=" + name + ", age=" + Age + ", job=" + Job + "]";} Public person () {super ();} 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 Getjob () {return job;} public void Setjob (String job) {this.job = job;} @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 (Job);//TODO auto-generated method stub}public static final creator<person> Creator = new CreaTor<person> () {@Overridepublic person Createfromparcel (Parcel source) {//TODO auto-generated method Stubreturn New Person (source.readstring (), Source.readint (), source.readstring ());} @Overridepublic person[] NewArray (int size) {//TODO auto-generated method Stubreturn new Person[size];}};}

Passing objects:

To pass an object in mainactivity:

Intent Intent = new Intent (mainactivity.this,receivactivity.class); person person = new person ();p erson.setname ("Zhangsan");p erson.setage;p erson.setjob ("IOS"); Intent.putextra (" Person ", person); startactivity (intent);

To remove an object from the receiveactivity:

Person person = getintent (). Getparcelableextra (' person ');//Receive Object if (person!=null) {Tv1.settext ("The object received is:" + Person.tostring ());}
Show results such as the following:



Delivery Collection:

To pass a collection in mainactivity:

Intent Intent = new Intent (); arraylist<person> persons = new arraylist<person> (); person person = new Person ("Wangwu", "Android"); Person Person1 = new Person ("Lisi", +, "PHP"); Person Person2 = new Person ("Zhaoliu", "IOS");p ersons.add (person);p Ersons.add (Person1);p ersons.add (Person2); Intent.putparcelablearraylistextra ("Persons", persons); Intent.setclass (Mainactivity.this, Receivactivity.class); StartActivity (Intent);

To remove a collection in receiveactivity:

arraylist<person> persons = Getintent (). Getparcelablearraylistextra ("persons"); if (Persons!=null && Persons.size () >0) {StringBuilder sb = new StringBuilder (); for (person personlist:persons) {Sb.append ( Personlist.getname () + "= =" +personlist.getage () + "= =" +personlist.getjob () + "\ n");} Tv2.settext (Sb.tostring ());}
The results show demo samples such as the following:



2, serializable transfer data





Android Learning Path--android Four components activity (ii) data transfer

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.