Detailed explanation of Android programming to obtain global context and use intent to transfer objects

This paper describes the method of Android programming to obtain global context and use intent to transfer objects. Share with you for your reference, as follows:

1、 Get context globally

Context is needed in many places in Android development, such as pop-up toast, start activity, send broadcast, operate database

Since many operations are performed in activities, and the activity itself is a context object, it is not so difficult to obtain the context.

However, when the architecture of the application becomes more and more complex, a lot of logic code will be separated from the activity class. Therefore, in some cases, it is not so easy to obtain the context.

Android provides an application class. Whenever an application starts, the system will automatically initialize this class. We can customize our own application class to manage some global state information in the program, such as global context.

Here, we override the oncreate () method of the parent class, get an application level context by calling the getapplicationcontext () method, and then provide a static getcontext () method to return the context just obtained.

Next, you need to tell the system that when the program starts, you should initialize the myapplication class and specify it under the < Application > tab of the androidmanifest file.

In this way, a mechanism for obtaining the context globally has been implemented. No matter where you want to use the context in the project, you only need to call myapplication. Getcontext().

2、 Passing objects using intent

There are usually two implementations of using intent to pass objects, serializable and Parcelable.

Serializable mode:

Serializable means serialization, which means to convert an object into a storable or transportable state. Serialized objects can be transmitted on the network or stored locally. The serialization method is also very simple. You only need to let a class implement the serializable interface.

Here, let the festival class implement the serializable interface, so that all Festival objects can be serialized.

Here, we create an instance of the festival, then pass it directly into the putextra () method and pass the object through startactivity ().

Here, the getserializableextra () method is called to obtain the passed serialized object, and then it is transformed downward into a festival object. In this way, the function of passing objects using intent is successfully realized.

Parcelable mode:

The implementation principle of the Parcelable method is to decompose a complete object, and each part after decomposition is the data type supported by intent, so as to realize the function of passing objects.

First, let the person class implement the Parcelable interface, so you must override the describecontents () and writetoparcel () methods. The describecontents () method can directly return 0, while the writetoparcel () method needs to call the writexxx () method of parcel to write out the fields in the person class one by one.

In addition, a constant named creator must be provided in the person class. Here, an implementation of the Parcelable. Creator interface is created and the generic type is specified as person.

Next, you need to rewrite the createfromparcel() and newarray() methods. In the createfromparcel() method, read the name and age fields just written out, and create a person object for return. Both name and age are read by calling parcel's readxxx() method. Note that the reading order here must be exactly the same as the writing order. In the newarray () method, you only need to new out a person array and use the size passed in the method as the array size.

The same code can still be used to pass the person object, except that the getparcellableextra () method is called when getting the object, which is exactly the same elsewhere.

When transferring objects, both serializable and Parcelable methods are feasible. In contrast, the serializable method is relatively simple, but because the whole object will be serialized, the efficiency will be lower than the Parcelable method. These two methods should be used reasonably according to the situation.

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solving methods, summary of Android basic component usage, summary of Android view skills, summary of Android layout 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
分享
二维码
< <上一篇
下一篇>>