Getting started: 5. Data transfer between component 1:activity-activity

Source: Internet
Author: User
Tags object serialization

We have a basic understanding of the concept of activity in Android, Jump, and several startup modes, however, in real-world applications do not simply start or jump activity, usually in the start of a new activity always carry a variety of data or packets.

1. Pass a simple data type

Jump from activity aaty to Baty and carry basic data types

Aaty:

New Intent (Aaty.  this, Baty. class);         // Initializing Data I.putextra ("Data", "Test"); startactivity (i);

Baty:

Intent i = getintent ();                         // Obtaining Data I.getstringextra ("the");

Use the intent object as the carrier of the data.

2. Passing a packet

Jump from activity aaty to Baty and carry packets (bundle)

Aaty:

New Intent (Aaty.  this, Baty. class  New Bundle ();                  // Initialize Bundles b.putstring ("StringData", "Datatest"), B.putint ("Intdata",n); I.putextras (b); startactivity (i);

Baty:

Intent i = getintent ();                    // Get Bundles Bundle data = I.getextras (); Data.getstring ("StringData"); Data.getint ("Intdata") ;

3. Passing Value objects

(1) Using Java-brought value object serialization java.io.Serializable

Create a new custom class human class with two member variables age of type string name and int

First step: Enable the custom human class to implement the Serializable interface

Aaty:

New Intent (Aaty.  this, Baty. class ); I.putextra ("Human",new Human ("Zhangsan");startactivity (i) ;

Baty:

Intent i == (Human) I.getserializableextra ("Human");   // Gets the value object user.getname ();  // Working with Objects

Features: Java comes with serialization, the JVM is fully automated serialization, so the execution is less efficient. But the operation is simple, the code is few.

(2) The value object serialization of the android mechanism android.os.Parcelable

Aaty:

The first step: Create a new custom class human, and make the class implement the Parcelable interface

Step two: Implement rewrite two methods describecontents () and Writetoparcel (), the first method does not move, with its default on the line, mainly rewrite the second method:

Writetoparcel (Parcel dest,int  flags) {                         dest.writestring (GetName ());                         Dest.writeint (Getage ());                       }

Step three: Create a new creator

 Public Static FinalCreatorNewCreator() {@Override PublicHuman createfromparcle (Parcel source) {return NewHuman (sourse.readstring (), Source.readint ()); } @Override PublicHuman[] NewArray (intsize) {                     return NewUser[size];  }       }; //notice there's a semicolon here .

Fourth Step:

New Intent (Aaty.  this, Baty. class ); I.putextra ("Human",new Human ("Zhangsan", 20));

Baty:

Human Human = I.getparcelableextra ("Human");          // Get Value Object // working with value objects

4. Get Activity return parameters

Sometimes when we start an activity and pass the data, we need the activity of the latter to do the data analysis and give the return value back to the former activity.

Activity2: Click the button to execute:

New Intent (); I.putextra ("Data", Edittext.gettext (). toString ()); Setresult (//1) is the status code (result code) that passes the data (Can be customized, the reason for success failure or failure). finish (); // ends the current activity and passes the return value back to the original activity. 

Activity2:

Startactivityforresult (i,0); // 0 is the request code to pass the data            @Overrideprotectedvoid onactivityresult (int requestcode,int resultcode,intent data) {                super. Onactivityresult (Requestcode, ResultCode, data);                 = "The data returned by Activity2 is   " +data.getstringextra "

Getting started: 5. Data transfer between component 1:activity-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.