This paper describes in detail the method of complex parameter transfer between Android intent. Share to everyone for your reference, specific as follows:
Intent is a medium for passing arguments between activity and activity, between activity and service, and these two typically implement Java basic object types and string delivery.
In the actual project, the value between pages, in addition to the above several, often have to pass object object, List type, list<object> type and global variables and so on demand. This article is about how to pass these types of arguments.
I. Transfer of list<string> and list<integer>
The following is an example of passing list<string>, which sends the list<string> syntax:
Intent.putstringarraylistextra (key, list);
The syntax for receiving list<string> is:
List = (arraylist<string>) getintent (). Getstringarraylistextra (key);
Here is an example of an application:
============= send list<string>=============
arraylist<string> stringlist = new ArrayList<String > ();
Stringlist.add ("string1");
Stringlist.add ("string2");
Stringlist.add ("String3");
Intent Intent = new Intent ();
Intent.setclass (Listdemoactivity.this, stringlistactivity.class);
Intent.putstringarraylistextra ("liststring", stringlist);
StartActivity (intent);
==================== receives list<string>======================
arraylist<string> stringList = ( arraylist<string>) getintent (). Getstringarraylistextra ("liststring");
List<integer> Similar operation calls the following method can also implement send and receive:
Intent.putintegerarraylistextra (key, list);
List = (arraylist<integer>) getintent (). Getintegerarraylistextra (key);
Two, the use of serializable and parcelable two ways to pass the object
There are two ways to pass objects between Android's intent, one is bundle.putserializable (Key,object) and the other is Bundle.putparcelable (Key,object). The object in the method satisfies certain conditions, the former realizes the serializable interface, and the latter implements the Parcelable interface.
The following is the user class that implements the Serializable interface, named Serializableuser, which is simply a convenient way to distinguish the user class from the class name that implements the Parcelable interface, which is not recommended in actual development:
public class Serializableuser implements Serializable {
private String userName;
private String password;
Public Serializableuser () {
} public
Serializableuser (string userName, string password) {
This.username = UserName;
This.password = password;
}
Public String GetUserName () {return
userName;
}
public void Setusername (String userName) {
this.username = userName;
}
Public String GetPassword () {return
password;
}
public void SetPassword (String password) {
this.password = password;
}
}
The following is the user class that implements the Parcelable interface:
public class Parcelableuser implements parcelable {private String userName;
private String password;
Public Parcelableuser () {} public Parcelableuser (string userName, string password) {this.username = UserName;
This.password = password;
Public String GetUserName () {return userName;
} public void Setusername (String userName) {this.username = UserName;
Public String GetPassword () {return password;
} public void SetPassword (String password) {this.password = password; public static final parcelable.creator<parcelableuser> Creator = new creator<parcelableuser> () {@Over Ride public Parcelableuser Createfromparcel (Parcel source) {Parcelableuser Parcelableuser = new Parcelableuser (
);
Parcelableuser.username = Source.readstring ();
Parcelableuser.password = Source.readstring ();
return parcelableuser; @Override public parcelableuser[] NewArray (int size) {return new Parcelableuser[size];
}
};
@Override public int describecontents () {//TODO auto-generated method stub return 0; @Override public void Writetoparcel (Parcel dest, int flags) {//TODO auto-generated a stub dest.writes
Tring (UserName);
dest.writestring (password);
}
}
The syntax passed in two ways is:
Bundle.putserializable (key,object);
Bundle.putparcelable (Key,object);
The syntax that is received in two ways is:
object= (Object) getintent (). Getserializableextra (key);
object= (Object) getintent (). Getparcelableextra (key);
========== use Serializable and parcelable respectively to send object===============
serializableuser serializableuser = new Serializableuser ("user1", "123456");
Parcelableuser parcelableuser = new Parcelableuser ("User2", "654321");
Intent Intent = new Intent ();
Bundle Bundle = new Bundle ();
Bundle.putserializable ("Serializableuser", serializableuser);
Bundle.putparcelable ("Parcelableuser", parcelableuser);
Intent.setclass (listdemoactivity.this,objectactivity.class);
Intent.putextras (bundle);
StartActivity (intent);
==================== receive object======================
serializableuser serializableuser = (SerializableUser) Getintent (). Getserializableextra ("Serializableuser");
Parcelableuser Parcelableuser = (parcelableuser) getintent (). Getparcelableextra ("Parcelableuser");
It may be noted that the implementation of the serializable interface is to serialize objects, and then transfer, and Java Common Programming no obvious difference, and user does not need to change significantly, relatively simple. I also recommend it in this way.
However, the latter implementation of the Parcelable interface of the class is more complex, parcelable is what kind of thing?
Android offers a new type: Parcel, which is used as a container for encapsulating data, and the encapsulated data can be passed through intent or IPC. In addition to the basic types, only classes that implement the Parcelable interface can be placed in parcel.
Implementing the Parcelable interface requires three methods:
1) Writetoparcel method. This method writes the data of the class to the externally provided parcel.
Disclaimer: Writetoparcel (Parcel dest, int flags).
2) Describecontents method. Just return it directly to 0.
3 Static Parcelable.creator<t> interface, this interface has two methods:
Createfromparcel (Parcel in) implements the ability to create an instance of a class from in.
NewArray (int size) creates an array of type T, length of size, returnnew t[size]; This method is used by the external class to deserialize the array of this class.
The log test outputs the operating condition of the program, in Bundle.putparcelable ("Parcelableuser", Parcelableuser), when the publicvoid in the Parcelableuser class is invoked Writetoparcel (Parcel dest, int flags) method, and writes data to Dest in parcelableuserparcelableuser= (parcelableuser) getintent (). Getparcelableextra ("Parcelableuser"), the public parcelableusercreatefromparcel in the Parcelableuser class was invoked (Parcel source Method, creates a Parcelableuser object, assigns a value to the object's properties, where the parcel source and parcel dest are the same, and then returns the Parcelableuser object. Finally, you can print out the Parcelableuser attribute information.
Iii. Transfer of List<object>
What do we do if we're going to pass the list of Object, that is, list<object> First you need to implement the Object object serializable interface, and then convert the list coercion type to the serializable type, and finally through:
Intent.putextra (Key, (Serializable) objectList);
This syntax is passed, and the receiver needs to force the type to be converted to List<object> at the time it is received, and the syntax used by the receiving list<object> is:
Objectlist= (list<object>) getintent (). Getserializableextra (key);
Here is an example of an application where the Serializableuser class used here is given in the previous step and is not repeated here.
============== send list<object>===========
serializableuser user1 = new Serializableuser ("user1", "123456" );
Serializableuser user2 = new Serializableuser ("User2", "654321");
list<serializableuser> objectList = new arraylist<serializableuser> ();
Objectlist.add (user1);
Objectlist.add (user2);
Intent Intent = new Intent ();
Intent.setclass (Listdemoactivity.this, objectlistactivity.class);
Intent.putextra ("ListObject", (Serializable) objectList);
StartActivity (intent);
==================== receives list<object>======================
list<serializableuser> objectList = ( list<serializableuser>) getintent (). Getserializableextra ("ListObject");
Four, global variables
If some special application-level parameters are not convenient to use intent to pass parameters, it is easy to think of a global variable or static variable can be used? Static variables in Java are appropriate here, but their values are lost after the activity calls System.exit (0) or finish ().
And there's a more elegant way of using ApplicationContext in Android. This global variable method is more secure than the static class, until all of the applied activity is destory and then released.
As the Android SDK says, application is used to hold global variables and is present when package is created. So when we need to create a global variable, we don't need to create the static variable of public permissions like J2SE, but directly in the application. You can set or read the value of a global variable simply by invoking the getapplicationcontext of the context or the Getapplication method of the activity to obtain a Application object.
When application is started, the system creates a PID, the process ID, in which all activity runs. So we initialize the global variable when application is created, and all the activity of the same application can be taken to the values of these global variables, in other words, we change the values of these global variables in one activity, Then the values in the other activity in the same application will change.
Usage:
1. Create a subclass of your own android.app.Application, adding setter and getter methods for private global variables that you want to share.
public class MYAPP extends application{
private String globalvariable;
Public String getglobalvariable () {return
globalvariable;
}
public void setglobalvariable (String globalvariable) {
this.globalvariable = globalvariable;
}
}
2. Declare this class in manifest, where Android creates a globally available instance.
In fact, in the original only a application label for application to make a name for this global instance.
Copy Code code as follows:
<application android:name= ". MyApp "android:icon=" @drawable/icon "android:label=" @string/app_name ">
3. The Context.getapplicationcontext () method can be used anywhere else to obtain this instance, thereby obtaining the state (variable).
============ uses global variables to pass parameters ==============
MyApp MyApp = ((MyApp) Getapplicationcontext ());//Get our application MyApp
Myapp.setglobalvariable ("global variable");
Intent Intent = new Intent ();
Intent.setclass (Listdemoactivity.this, globalactivity.class);
StartActivity (intent);
============ receives the parameters of the global variable ==============
MyApp MyApp = ((MyApp) Getapplicationcontext ());
String globalvariable = myapp.getglobalvariable ();
For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Programming activity Operating Skills Summary", "Android Resource Operation skills Summary", "Android File operating skills summary" , "Android Operation SQLite Database Skills Summary", "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android programming development of SD card operation method Summary", " Android View tips Summary and a summary of the use of Android controls
I hope this article will help you with the Android program.