In Android, you can use intent when two of your activity needs to interact with information. Specifically:
To send a single type of data:
= "Hello secondactivity"new Intent (firstactivity. this, secondactivity. class ); Intent.putextra ("Extra_data", data); StartActivity (intent); #在onClick中调用接收方: Intent =Intent.getstringextra ("Extra_data"); #可以在onCreate中读取
In addition to Putextra ("key", "value")/getstringextra ("key"), there are many pairs depending on the type of value passed, such as:
PutExtra ("Key", Intvalue)/getintextra ("key") PutExtra ("key", Floatvalue)/getfloatextra ("Key") PutExtra ("key", Doublevalue)/getdoubleextra ("key")
PutExtra ("Key", Bundlevalue)/getbundleextra ("key")
...
Send a combination of multiple data types:
Bundles can also be used to send many data types (including nested bundles) to bundles:
Sender
New Bundle (); B.putint ("Age", 2); B.putstring ("Data", "Hello"); Intent.putextras (b); startactivity (intent);
Receiver
Intent i == I.getextras (); int age = b.getint ("age"= b.getstring ("Data");
To send a class object:
Method 1:serializable
Serializable is a serialization method provided by Java, with the advantage that it is simple to use and that the system is responsible for it, and the disadvantage is that it is slightly slower.
Public classUserImplementsserializable{PrivateString name; PrivateInteger age; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; } PublicUser (String name, Integer age) { This. Name =name; This. Age =Age ; }}
Sender
New User ("JM", 30));
// Receiver Intent i == (user) I.getserializableextra ("User"); Textview.settext (String.Format ("name:%s, Age:%s ", User.getname (), User.getage ()));
Method 2: Parcelable
Public classUserImplementsparcelable{PrivateString name; PrivateInteger age; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; } PublicUser (String name, Integer age) { This. Name =name; This. Age =Age ; } @Override Public intdescribecontents () {return0; } @Override Public voidWritetoparcel (Parcel Parcel,inti) {//Write All the fields this want to sendparcel.writestring (GetName ()); Parcel.writeint (Getage ()); } Public Static FinalCreator<user> Creator =NewCreator<user>() {@Override PublicUser createfromparcel (Parcel Parcel) {return NewUser (parcel.readstring (), Parcel.readint ()); } @Override PublicUser[] NewArray (inti) {return NewUser[i]; } };}
// Sender New User ("JM", +)); StartActivity (intent);
// Receiver User user = (user) I.getparcelableextra ("User"); Textview.settext (String.Format ("name:%s, Age:%s", User.getname (), User.getage ()));
If there are multiple fields of the same type in user, such as string, it can be implemented with bundles:
@Override Public voidWritetoparcel (Parcel Parcel,inti) {//Write All the fields this want to send//parcel.writestring (GetName ());//Parcel.writeint (Getage ());Bundle B =NewBundle (); B.putstring ("Name", GetName ()); B.putint ("Age", Getage ()); Parcel.writebundle (b); } Public Static FinalCreator<user> Creator =NewCreator<user>() {@Override PublicUser createfromparcel (Parcel Parcel) {//return new User (Parcel.readstring (), Parcel.readint ());Bundle B =Parcel.readbundle (); return NewUser (b.getstring ("name"), B.getint ("Age")); } @Override PublicUser[] NewArray (inti) {return NewUser[i]; } };
To get the results back from another activity:
Start-up:
Startactivityforresult (Intent, 0);
0 indicates the request code, indicating what the intent of this request is.
Receiving end:
New Intent (); I.putextra ("Data", Edittext.gettext (). toString ()); Setresult (RESULT_OK, i); Finish ();
Transfer of data in Android