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