In-depth analysis of Android Activity

Source: Internet
Author: User


From:

Http://mobile.51cto.com/android-266690.htmhttp://www.linuxidc.com/Linux/2012-02/53485.htm
Activity is an application component that provides users with a visual interface for convenient operations, such as calling, taking photos, sending emails or browsing maps. Each activity provides a visual window, which overwrites the entire screen, however, in some cases, some smaller windows may float on another window. Similar to the concept in windows, activity corresponds to a dialog (MFC) or Form (C #), which provides users with a visual interface. Overview activities is one of the components of a program. Its main function is to provide interfaces. A program generally consists of multiple activities. The relationships between various activities are loose and there is no direct association between them. An activity must be specified as the main activity, which is the first interface displayed when the program starts. Every activity can start other activities at will. When an activity is started, the previous activity is stopped. All started activities in a program are put in a stack, so the stopped activity is not destroyed and stored in the slave. The newly started activity is first stored in the stack and then the input focus is obtained. Click the return key on the activity of the current activity. It is retrieved from the activity and destroyed. The previous activity is restored. When an activity is stopped because the new activity is started, it will receive notifications of status changes. There are multiple such changes, each callback method will cause the system to call a corresponding callback method to notify the activity. These callback methods are collectively referred to as "lifecycle callback methods ". These callback methods are called when an activity is created, stopped, restored, or destroyed. How to Create activity1 to inherit from the class activity. 2. The two most important methods for implementing the "Life Cycle callback method" are oncreate () -- this is a required function and Initialization is performed in it. Remember: you must call the setcontentview () function in this function to set the activity interface. Onpause () -- this is important, but it is not required. This function is called when the user leaves the activity (this generally does not mean that the activity will be destroyed ). In this function, you generally need to submit the data that needs to be saved (because the user may not return to this activity ). Other callback methods are implemented as needed. Implementing the user interface here refers to the controls on the activity. All controls are derived from the View class, so they can all be called views. Each widget occupies a rectangle. Controls are classified into the following two types: widgets is a control that completes specific functions, such as button, text field, and checkbox. Layouts is a widget that can contain and typeset the widgets control. Of course, layout can also contain layout. Widgets is derived from the View class, and layouts is derived from the viewgroup class. developers can create their own controls from the view or viewgroup class. The best way to define the interface is to use the layout file in XML format, which is saved as resources in the project and can be found under Res/layout of the project. In this way, code and boundaries are separated. To set an XML file in layout as an activity interface, call setcontentview () of the activity and pass the XML resource ID as a parameter. Declare an activity in the list file to allow the system to operate your activity, you must declare it in the list file of the project. Example: <manifest...>
<Application...>
<Activity Android: Name = ". exampleactivity"/>
...
</Application...>
...
</Manifest> Of course, many attributes can be set to activity, such as label, icon, or topic. For more information, see the description of the <activity> element. See <activity Android: Name = ". exampleactivity "/>, see the value of activity name, there is ". ", if you forget it, the program runs incorrectly, and it is difficult for you to find out the cause of the error. Second, whether you are using an activity only internally or externally, you must register it in the list file. Otherwise, an inexplicable error will still occur, but during internal use, you do not need to add intent filters for acitivity. You can use the intent transmitter to specify multiple senders for a <activity> element. Use the <intent-filter> element. The intent timer is used to tell other components how to start the activity. When you use ADT to create a new project, the root activity is automatically created. The root activity already has two intent inspector. One intent inspector declares that the activity is responsible for responding to the "Main" action; another handler declares that this activity must be placed under the "launcher" category. This is generally the case: <activityandroid: Name = ". exampleactivity" Android: icon = "@ drawable/app_icon">
<Intent-filter>
<Actionandroid: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity> <intent-filter> indicates the filters. <Action> indicates that this acitivity is the main entry of the program. <Category> it indicates that this acitivity must be listed in the Application List of the system. If the activity in the program you write does not need to be called by other programs, you do not need to add any intent filters for the activity. However, the program must have an activity specified as "Main" action and "launcher" category. The activity in your own process can be called in a more direct way. However, if you want your activity to be called by other programs, you need to add an intent filter for it. These intent filters include <action>, <Category>, and <DATA>. These elements indicate the type of intent that your activity responds. To start an activity, you can use startactivity () to start an activity. The parameter intent indicates the activity to be called. In intent, you can specify the activity to be started or only the activity type. In this case, the system selects an appropriate activity for you, which may be located in other programs, it may also be in your own program. Intent can contain a piece of data used by the new activity (equivalent to passing parameters ). To start an internal activity within your program, you must specify the Class Name of the new activity in intent. Example: intent = new intent (this, signinactivity. Class );
Startactivity (intent); signinactivity is the activity class to be started. However, your program may want to execute functions that it does not provide, such as sending emails and sending short messages. In this case, you need to start the activity provided by other programs. In this case, the real value of intent comes: it can easily start the activity provided by other programs. You only need to specify the action you want to execute in intent, then call startactivity (), and the system will select an appropriate activity for you and start it according to your needs. If multiple activities can be executed at the same time, you can select which activity to use. For example, if you want a user to send an email, you can create the following intent: intent = new intent (intent. action_send );
Intent. putextra (intent. extra_email, recipientarray );
Startactivity (intent); putextra () is set to expand the data,. extra_email indicates that the second parameter recipientarray contains multiple email addresses. When the email sending program is started and receives the intent, it puts the email addresses in the "to" control on its acitivity interface. When the user sends a response, your activity resumes running (resume ). We can see how easy it is to start another program. Start an acitvity and get the result. Sometimes, you may want to get the result returned after the activity you start is executed. In this case, you can use startactivityforresult () to start the new acitivity (no longer startactivity ). Then, your program also needs to define the callback method onactivityresult (). When the new activity ends, it returns an intent to your program, which is received in onactivityresult. For example, if you want a user to open the address book, select a contact from it, and then obtain and process the selected contact. The following is the sample code: Private void pickcontact (){
// Create anintent to "pick" a contact, as defined by the content provider URI
Intent intent = new intent (intent. action_pick, contacts. content_uri );
Startactivityforresult (intent, pick_contact_request );
}
 
@ Override
Protected void onactivityresult (INT requestcode, intresultcode, intent data ){
// If the requestwent well (OK) and the request was pick_contact_request
If (resultcode = activity. result_ OK & requestcode = pick_contact_request ){
// Perform aquery to the contact's content provider for the contact's name
Cursorcursor = getcontentresolver (). Query (data. getdata (), new string [] {contacts. display_name}, null );
If (cursor. movetofirst () {// true if the cursor is not empty
Intcolumnindex = cursor. getcolumnindex (contacts. display_name );
String name = cursor. getstring (columnindex );
// Dosomething with the selected contact's name...
}
}
} This example shows the basic logical flow in onactivityresult. First, check whether the started activity is running correctly. If the resultcode is activity. result_ OK, it indicates that it is normal. Second, check whether the requestcode is consistent with the request at the time, that is, whether it is pick_contact_request. After the data passes, start to operate the returned data, that is, the data parameter. Data is processed in this way. contentresolver is used to send a request to the content provider. This request returns a cursor through which data is read, which is similar to database table operations. For more information, see the Content Providers section. You can call the finish () method internally to close an activity, or call the finishactivity () method to close other activities. Note: In most cases, you should not take the initiative to end an activity. The system is in charge of the life of the activity, so you do not have to end your own activity. Using the above method will damage the user experience. Do not do this unless you think it is necessary! This article from the Linux community website (www.linuxidc.com) original link: http://www.linuxidc.com/Linux/2012-02/53485.htm

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.