Android fourth page concept roaming (bottom)

Source: Internet
Author: User

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

In the third night of Android concept roaming (above), I introduced the basic function unit of Android and the intent connection method. On top of this skeleton, we can further add some important concepts that are closely related to development.

Context

An application consists of a number of functional units, such as activity and service. An app share has a application Context object. Within a functional unit, you can invoke the object in a specific way. As the name indicates, the context object records the environment in which the application is located. Like a newborn's functional unit, you can find out about the current perimeter, such as the size of the screen, by querying the context object. This context object also contains a number of application-level features, such as loading application resources. A context object coexists with the corresponding application. It is created at the beginning of the app's run, until the app finishes running. Throughout the process, the functional unit of the application seems to be bathed in the application context of this warm pool, ready to enjoy the comfort of bathing.

Context

In addition to the only application context object in an application, it is possible to create additional context objects during the application run. Both the activity class and the service class inherit from the context class, so the activity object and service object are also context objects, which I call the activity context and service Context. Both of these context objects have the same life cycle as the corresponding activity object and service object. They provide peripheral information and functionality for internal objects. For example, when creating a UI element view, you need to pass the activity context object as a parameter to the view. At the end of the activity, the corresponding activity context object is also destroyed. Within the activity and service, the corresponding activity context and service context object can be invoked through this. The activity context object provides the ability to start a new activity, while the service context object has no UI-related functionality.

Different types of context objects have a lot of overlap in functionality, such as the ability to start a service. But be aware of the differences in their life cycles. The application context object has the longest life cycle. Over-reliance on application context objects can lead to memory leaks. Therefore, in principle, use application context and service context objects as much as possible to implement the function of the context class.

Life Cycles

View

Activity is a screen interface. There are always various view elements on the interface, such as a button, a line of text, and an input field. Each view element is represented in wirelessly as a view class, such as the button class, the TextView class, and the EditText class. Android's view elements are diverse, and even images and Web pages have corresponding view elements. In addition to the specific view elements, the view class also has a viewgroup subclass. Multiple view objects can be attributed to a ViewGroup object, while ViewGroup objects often define the layout relationships of subordinate view elements, such as a top-down linear arrangement, or a grid-point arrangement. With this attribution, all the view elements and the abstract layout of an activity form a view tree. This tree contains the view information for the entire screen.

View Tree

In the Android design, we tend to add a variety of attributes to the view element to control their text, size, size, color, and other information. For ViewGroup, it has more than one sub-class, which represents a different layout style. It is more common to have linear layout linearlayout and relative layout relativelayout. Linear layout Arranges the view elements linearly in one direction, whereas relative layouts define the relative position of the view element and the parent view object or the sibling view object. By nesting a variety of layouts, we can create complex interface layouts.

In one activity, we can create a view tree programmatically by creating a new View object. The properties of a view element can be adjusted by means of the View object. But the more common approach is to define an XML file. Use XML's natural tree structure to represent a view tree. An XML node corresponds to a View object, and the properties of the View object can be recorded in the properties of the node. Android can understand this XML definition and instantiate the XML node as an in-memory object, which is the process of layout inflate. For example, the following XML record:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >    <TextViewAndroid:id= "@+id/text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Hello, I am a TextView" />    <ButtonAndroid:id= "@+id/button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Hello, I am a button" /></LinearLayout>

It represents a linear layout that contains a text view and a button.

Since 3.0, Android has introduced Fragment. Fragment, like activity, contains a view tree. Because an activity can contain multiple fragment, which is multiple view trees, this increases the flexibility of the interface design. After all, activity can contain only one view tree before fragment, which is more rigid. With fragment, the programmer can select the included view tree based on the current state of the activity, such as displaying a fragment on the phone screen and displaying two fragment on the tablet. In this way, the design of the view can also be reused without the use of different devices.

Fragment operates in a similar way to activity, and is also called OnStart () in state transitions.

Resource

In Android development, it is possible to require some static resources, such as slices, strings, XML files, and so on. These resources are stored in different folders under the res/of the Android project.

Different resources are classified and stored. We mentioned the use of XML to define layouts, which are stored in layout. The drawable* folder contains images of different sharpness, while strings are stored in the Strings.xml under values. In the Android project, we can use the ID or file name to refer to the corresponding resource.

Summary

Context: environmental information and functions

View: Views

Resource: Static Resources

Welcome to the "Java Quick Tutorials" article series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.