Detailed explanation of complex parameter transfer methods between Android intents

This paper describes in detail the complex parameter transfer method between Android intents. Share with you for your reference, as follows:

Intent is the medium for transferring parameters between activities and between activities and services. These two methods usually realize the transfer of java basic object types and strings. In actual projects, in addition to the above, there are often requirements for transferring values between pages, such as object object, list type, list < Object > type and global variables. This article introduces how to pass these types of parameters.

1、 Pass list < string > and list < integer >

The following takes the passing list < string > as an example, and the syntax of sending list < string > is:

The syntax of receiving list < string > is:

The following is an application example:

List < integer > similar to the above operations, sending and receiving can also be realized by calling the following methods:

2、 Use serializable and Parcelable methods to pass objects

There are two methods to transfer objects between Android intents. One is bundle putSerializable(Key,Object); The other is bundle putParcelable(Key,Object)。 The object in the method must meet certain conditions. The former implements the serializable interface and the latter implements the Parcelable interface.

The following is the user class that implements the serializable interface. The name serializableuser is purely to distinguish the user class that implements the Parcelable interface from the class name, which is not recommended in actual development:

The following is the user class that implements the Parcelable interface:

The syntax passed in two ways is:

The syntax of receiving in two ways is:

It may be noted that the implementation of serializable interface is to serialize objects and then transmit them. It is no obvious difference from common Java programming, and the user does not need to be significantly changed. It is relatively simple. I also recommend this way.

However, the latter class that implements the Parcelable interface is more complex. What is Parcelable?

Android provides a new type: parcel, which is used as a container for encapsulating data. The encapsulated data can be transmitted through intent or IPC. In addition to the basic types, only classes that implement the Parcelable interface can be placed in parcel.

Three methods are required to implement the Parcelable interface:

1) Writetoparcel method. This method writes the data of the class to the externally provided parcel. Declaration: writetoparcel (parcel DeST, int flags).

2) Describecontents method. Just return 0 directly.

3) Static Parcelable Creator < T > interface, which has two methods:

Createfromparcel (parcel in) implements the function of creating an instance of a class from in. Newarray (int size) creates an array of type T and length size, returnew t [size]; Just. This method is used by the outer class to deserialize this class array.

The operation of the program can be known from the log test output. In the bundle putParcelable("parcelableUser",parcelableUser); Call the publicvoid writetoparcel (parcel DeST, int flags) method in the parcelableuser class and write data to dest. When parcelableuser = (parcelableuser) getintent() getParcelableExtra("parcelableUser"); When, call the public parcelableusercreatefromparcel (parcel source) method in the parcelableuser class, create a parcelableuser object, assign values to the properties of the object, where the parcel source and parcel dest are the same, and then return the parcelableuser object. Finally, you can print out the attribute information of parcelableuser.

3、 Pass list < Object >

What if we want to pass a list composed of objects, that is, list < Object >? First, implement the serializable interface for the object object, then convert the list mandatory type to the serializable type, and finally:

The receiver also needs to force the type conversion to list < Object > when receiving. The syntax used for receiving list < Object > is:

The following is an application example. The serializableuser class used here is given in the previous step and will not be repeated here.

4、 Global variable

If some special application level parameters are inconvenient to use intent to pass parameters, we can easily think whether there are global variables or static variables to use? The static variable in Java is suitable here, but its value is in the activity calling system Lost after exit (0) or finish ().

In Android, a more elegant way is to use ApplicationContext. This global variable method is more secure than static classes. It will not be released until all the applied activities are destroyed.

The Android SDK says that application is used to save global variables and exists when the package is created. Therefore, when we need to create global variables, we do not need to create static variables with public permissions like J2SE, but directly implement them in the application. You only need to call getapplicationcontext of context or getapplication method of activity to obtain an application object, and you can set or read the value of global variables.

When starting the application, the system will create a PID, that is, the process ID, and all activities will run on this process. Then we initialize global variables when an application is created, and all activities of the same application can get the values of these global variables. In other words, if we change the values of these global variables in an activity, the values of other activities in the same application will change.

Usage:

1. Create your own Android app. Subclass of application, add setter and getter methods for the private global variables you want to share.

2. Declare this class in the manifest, and Android will create a globally available instance for this.

In fact, it is to specify a name for the application on the original only application tag as the global instance.

3. Context can be used anywhere else The getapplicationcontext () method gets the instance, and then gets the state (variable) in it.

For more Android related content, readers who are interested can view the special topics of this site: introduction and advanced tutorial of Android development, activity operation skills summary of Android programming, Android resource operation skills summary, Android file operation skills summary, Android SQLite database operation skills summary, Android JSON format data operation skills summary Summary of Android database operation skills, summary of SD card operation methods for Android programming development, summary of Android view skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>