First look at the official API of intent.
Here's how to pass some basic types of data:
- PutExtra (String name, int value)
- PutExtra (string name, String value)
- PutExtra (String name, float value)
- PutExtra (String name, double value)
- PutExtra (String name, Long value)
- PutExtra (String name, Boolean value)
- PutExtra (String name, byte value)
- PutExtra (String name, char value)
The method of passing the base type ArrayList object is as follows:
- Putintegerarraylistextra (String name, ArrayList "Integer" value)
- Putstringarraylistextra (String name, ArrayList "Integer" value)
However, we do not find a way to pass the object type directly, but in development we often need to pass an object type or ArrayList "object" type of data, so how should we implement it?
Here's how:
Pass the object type:
- PutExtra (String name, Serializable value)
- PutExtra (String name, parcelable value)
Pass ArrayList the type of Object:
- PutExtra (String name, Serializable value)
- PutExtra (String name, parcelable value)
- Putparcelablearraylistextra (String name, ArrayList "? Extends Parcelable" value)
Here we pass the student object from activity to service.
Student class
PackageCom.example.intenttest; Public class Student implements Serializable{ PrivateString name;PrivateString sex;Private intId Public Student(string name, String sex,intID) {Super(); This. name = name; This. sex = sex; This. id = ID; }@Override PublicStringtoString() {return "Student [name="+ name +", sex="+ Sex +", id="+ ID +"]"; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } PublicStringGetsex() {returnSex } Public void Setsex(String Sex) { This. sex = sex; } Public int getId() {returnId } Public void setId(intID) { This. id = ID; }}
Activity class:
PackageCom.example.intenttest;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle; Public class mainactivity extends Activity { @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Create student ObjectStudent Student =NewStudent ("Small grey ash","Male",1);//Pass object to service via intentIntent Intent =NewIntent (mainactivity. This, Myservice.class); Intent.setaction ("Action"); Intent.putextra ("Student", student); StartService (Intent); }}
Service class:
PackageCom.example.intenttest;ImportAndroid.app.Service;ImportAndroid.content.Intent;ImportAndroid.os.IBinder;ImportAndroid.util.Log; Public class myservice extends Service{ @Override PublicIBinderOnbind(Intent Intent) {//TODO auto-generated method stub return NULL; }@Override Public int Onstartcommand(Intent Intent,intFlagsintStartid) {if("Action". Equals (Intent.getaction ())) {Student Student = (Student) Intent.getserializableextra ("Student"); LOG.I ("Tag","ToString () of the Student object:"+student); }return Super. Onstartcommand (Intent, flags, Startid); }}
Results:
pass ArrayList object objects from Mainactivity to secondactivity:
Mainactivity class:
PackageCom.example.intenttest;ImportJava.util.ArrayList;ImportJava.util.List;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle; Public class mainactivity extends Activity { @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Must be ArrayList here, cannot be a listarraylist<student> list =NewArraylist<student> ();//Create student ObjectStudent Student1 =NewStudent ("Small grey ash","Male",1); Student Student2 =NewStudent ("The Big gray Wolf","Male",2); Student Student3 =NewStudent ("Red Wolf","female",3); List.add (STUDENT1); List.add (Student2); List.add (STUDENT3);//Pass object to service via intentIntent Intent =NewIntent (mainactivity. This, Secondactivity.class); Intent.setaction ("Action"); Intent.putextra ("Studentlist", list); StartActivity (Intent); }}
(Note: The Mainactivity class must use ArrayList instead of list when declaring the list, otherwise there will be a hint error message,
)
Secondactivity class:
PackageCom.example.intenttest;ImportJava.util.ArrayList;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.util.Log; Public class secondactivity extends Activity { @Override protected void onCreate(Bundle savedinstancestate) {//TODO auto-generated method stub Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Intent Intent = Getintent ();if("Action". Equals (Intent.getaction ())) {arraylist<student> list = (arraylist<student>) intent.getserializabl Eextra ("Studentlist"); for(inti =0; I<list.size (); i++) {LOG.I ("Tag","Student:"+ List.get (i)); } } }}
Operation Result:
The above is the code that uses the Putextra (String name, Serializable value) method to carry out the passing of object and ArrayList "object".
Via PutExtra (string name, parcelable value) and Putparcelablearraylistextra (string name, ArrayList "? Extends Parcelable" Value) Two methods are used in the same way as Putextra (String name, Serializable value), except in the same way as serialization.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Intent passing object and Arraylist<object> objects in Android---notes