"Alearning" chapter fifth introduction to Android related components (i) Activity

Source: Internet
Author: User

Android apps are made up of fragmented, connected components that are bundled together through a project manifest. In manifest, the role of each component and component is described, with 6 components, which are the cornerstone of Android applications.

    • Activities (Event)
    • Service (services)
    • Content Provider (contents provider)
    • Intent (intention)
    • Broadcast receiver (broadcast receiver)
    • Notification (notice)
Here we refer to the concept of components (Component), where components (Component) are a simple encapsulation of data and methods, and components can have their own properties and methods. Properties are simple accessors of component data. The method is a simple and visible function of the component. Using components enables drag-and-drop programming, fast property handling, and true object-oriented design. When it comes to the so-called architecture and reuse, components are a difficult concept to avoid. Let's expand here and learn about Android's component-oriented thinking.

"blog column:http://blog.csdn.net/column/details/alearning.html"

"The following excerpt from CSDN blog: http://blog.csdn.net/luoxinwu123/article/details/8019547"

The component-oriented idea is a kind of software design thought based on object-oriented in the background of expanding software scale and increasing complexity. It can be understood as a more coarse-grained object-oriented, its granularity is generally greater than the object, but to what extent, and can be determined according to the actual situation. This idea is based on components, emphasizing the concept of "service".
Component-oriented has the following characteristics:
Low coupling: The components generally do not depend on each other, a component only need to know the "name" of the other component can access it;
High reusability: System-level reuse;
High interoperability: The components developed by different developers can access each other as long as the interfaces are defined;
Transparency of processes: components can work in the same process can also work in different processes;
The independence of the language and development environment: components only need to define a good service interface, the internal implementation can be in any language.
Component-oriented frameworks are already numerous, such as JavaBean, EJB, COM, and so on. Android does not claim to be component-oriented, but from the perspective of its design, the first design consideration of Android is to adopt a "borderless" design approach, in order to achieve a high level of reuse of resources within the system.
In fact, Android is fully compliant with component-oriented features. First, Android offers four components of activity, Service, Broadcastreceiver, and ContentProvider. The collaboration between these four components is based on a binder mechanism, where servicemanager and Activitymanagerservice are the most important foundations for working together between components, and they are also components themselves. ServiceManager is a binder daemon that manages various services and provides the ability to query remote interfaces for clients that invoke these services. Activitymanagerservice is responsible for all activity and service launches, as well as the registration and distribution of all broadcasts in the system and the sending of broadcasts to recipients.
In This chapter, we will introduce the concept, knowledge point and usage of the basic components in the way of case. The first basic component of activity learning is then performed together.

activity (Activities)

The first thing we need to understand about activity is that activities are an interface between the user and the application, that is, a graphical interface that provides a visualization, and a container for a set of controls, that is, we can add the controls we need to the activity interface.
Creating an activity requires inheriting the Android.app.Activity object to generate the corresponding subclass. In general, we need to rewrite (overwrite) the method

A more in-depth understanding of activity requires understanding and explaining the activity's life cycle. First look at the life cycle diagram of the activity on the Android developers web, as shown in:



In Android, Activity has four basic statuses:
1. active/runing: After a new activity is placed on the stack, it is at the top of the screen, at the topmost end of the stack, and is in a visible and interactive activation state.
2. Paused: State when activity is overwritten by another transparent or Dialog-style activity. At this point it remains connected to the window manager and the system continues to maintain its internal state, so it is still visible, but it has lost focus and cannot interact with the user.
3. stoped: State when activity is overwritten by another activity, loses focus, and is not visible.
4. killed:activity is killed by the system when it is recycled or is not in the boot state.


The life cycle of activtiy involves methods:
? protected void OnCreate (Bundle savedinstancestate): The first method called when an instance of an Activity is started. In general, we cover this method as an entry point for the application, where we do some initialization data, set up the user interface, and so on. In most cases, we're going to load a well-designed user interface from XML here.
? protected void OnStart (): The method is called after the OnCreate () method, or when the Activity transitions from the Stop state to the active state.
? protected void Onresume (): Called when Activity transitions from the Pause state to the active state. Generally do data recovery work in this piece.
? protected void OnPause (): Called when Activity transitions from the active state to the pause State. In general, we save the status information of the Activity here.
? protected void OnStop (): Called when Activity transitions from the active state to the Stop state.
? protected void OnDestroy (): Called at the end of Active, it is the last method called at the end, where it is generally done to release resources, clean up memory, and so on.
? protected void Onrestart (): Called when Activity transitions from the Stop state to the active state and is not used frequently.
After you create the activity, you need to register the activity you created in the Androidmainifest.xml file, in this case the activity is testactivity.

<activity            android:name= "cn.mahaochen.app.alearning.chapter5.TestActivity"            android:screenorientation = "Portrait" ></activity>

Here we expand the knowledge and review the original knowledge point, about the Androidmainifest.xml property configuration.

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.mahaochen.activity "android:versioncode=" 1 "android:versionname=" 1.0 "> <applicati On android:icon= "@drawable/icon" android:label= "@string/app_name" > <!--. Means Mainacti Vity is the class under current package com.mahaochen.activity.              If the class is under the current package of the application, the dot symbol can be omitted, if the class must be dotted under the sub-package applied, such as: Mainactivity class under the Com.mahaochen.activity.user package can be written like this: <activity Android:name= ". User. Mainactivity "/>---<activity android:name=". Mainactivity "android:label=" @string/app_name "> <!--1, an application can have multiple activity, each a                 Ctivity is at the same level, which activity is started first when the program is started? Some programs may need to be displayed in the list of programs, some of which are not required.                  How do you define it?                 Android.intent.action.MAIN determines the activity Android.intent.category.LAUNCHER the application starts first determines whether the application appears in the program list 2, because your program may have a lot of activity as long as the XML configuration file has soA intent-filter, and there is this launcher, then this activity is the activity that first runs when the program is clicked. 3, now there is only one activity, then add no matter.              Used to set the activity to open as default when the emulator starts.                -<intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </ac tivity> </application> <uses-sdk android:minsdkversion= "7"/></manifest>

Case Procedure

"Testactivity.java"

import cn.mahaochen.app.alearning.r;import Android.app.activity;import Android.os.bundle;import Android.util.log;public class Testactivity extends activity{private final static String TAG = "M Ahaochen_testactivity ";/** * typically covers this method, loading the specified Contentview resource layout */@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_chapter5_activity); LOG.E (TAG, "activity_oncreate");} @Overrideprotected void OnStart () {Super.onstart (); LOG.E (TAG, "Activity_onstart");} @Overrideprotected void Onresume () {super.onresume (); LOG.E (TAG, "Activity_onresume");} @Overrideprotected void OnPause () {super.onpause (); LOG.E (TAG, "Activity_onpause");} @Overrideprotected void OnStop () {super.onstop (); LOG.E (TAG, "Activity_onstop");} @Overrideprotected void OnDestroy () {Super.ondestroy (); LOG.E (TAG, "Activity_ondestroy");} @Overrideprotected void Onrestart () {Super.onrestart (); LOG.E (TAG, "Activity_onrestart");}} 

"Activity_chapter5_activity.xml"

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <textview        android:layout_width= "match_parent"        android:layout_height= " Wrap_content "        android:gravity=" center "        android:text=" activity learning "/><imageview android:src=" @ Drawable/chapter5_activity_lifecycle "    android:layout_height=" wrap_content "    android:layout_width=" Match_parent "/></linearlayout>

effect



References1, http://blog.csdn.net/bage1988320/article/details/6855774
2, http://www.oschina.net/question/54100_27841
3, http://blog.csdn.net/android_robot/article/details/6906243

Related Article

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.