[Android training video series] 2.1 starting an activity

Source: Internet
Author: User

1. Main Content
This section briefly introduces the lifecycle of an activity, describes how to set the main activity, and finally describes how to create and destroy an activity.

2. Video description

Http://www.eyeandroid.com/thread-11253-1-1.html

 

3. Translation Reference

 

Start Activity

 

Unlike other functions that start with the main () function in an application, the startup code of the Android system is carried out in a specific callback method of an activity instance by calling a specific stage corresponding to its lifecycle.
There are a series of methods to start the activity, and a series of methods to cancel an activity. This tutorial will describe the most important function methods in the life cycle and show you how to process the primary life cycle callback function when creating your activity instance.


Measure the test taker's knowledge about lifecycle callback functions.

Throughout the life of an activity, the system calls a series of pyramid-like Life Cycle functions in sequence. That is to say, the activities of each stage are a separate step in the pyramid. When the system creates a new activity instance, every time a function is called, a step is taken to the top of the pyramid. The top of the pyramid is the status in which the activity is running at the front end and the user is interacting with it. When the user begins to leave the activity, the system will call other methods to make the activity state go to the lower end of the pyramid and gradually remove the activity.
Under some conditions, the activity only takes a small step to the low end and waits for it (for example, the user switches to another application ), at this point, the activity can return to the top (when the user returns to the original activity) and return to the original state.

Figure 1. A simplified diagram of activity lifecycle is like a step pyramid. This image shows how the callback function is used in each State to bring the restoration status back to the top or the descent status to the bottom. Activity can be restored from paused and stopped to resumed. Depending on the complexity of your activity, you may not need to use all lifecycle functions. However, it is very important to recognize every cyclic function and use them to meet users' expectations.

Correct use of your activity lifecycle function to ensure the good performance of the application, you must pay attention to many aspects, including the following content:
Do not let the program crash when the user calls or transfers to another application.
When a user does not activate it, it does not consume valuable system resources.
When a user leaves your application and returns the result later, the user's progress will not be lost.
When the user screen is switched horizontally or vertically, the user progress will not crash or be lost.

Resumed status

In this status, the activity runs on the foreground, and you can interact with it. (Sometimes called "running .)

Paused status

In this status, the activity is partially masked (by other activities that are translucent at the front end or do not cover the entire screen ). This status does not accept user input and cannot execute any code.

Stopped status

In this state, the activity is completely hidden and invisible, and is considered to be in the background. Although it is stopped, the active instance and all member variables such as status information will be retained, but no code can be executed.
Other States (created and started) are very short and the system quickly switches to the next state by calling the function.
Therefore, after the system calls oncreated (), the onstart () method is quickly called to enter the next state, and onresumed is immediately called to enter the next state.

 

The above is the basic life cycle knowledge. Next, you start to learn some specific lifecycle behaviors.

Specifies the activity that your program starts for the first time.

When you click the application icon from the main screen, the system will call the oncreate () function of the launcher or main activity you declare. This is the main entry point of activity as your application interface. You can define the main activity of a program in the androidmanifest. xml file, which is under the root directory of your project.

The main activity must use the <intent-filter> label in the manifest file and contain the main action and launcher content. For example:

<Activity Android: Name = ". mainactivity" Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>

Note: When you create a new Android project, the default project will contain an activity class, and this class will have the above declaration. If the mainaction content or launcher content is not in any of your activity life, your application icons will not appear in the main screen menu list of Android.


Create a new instance

Most applications have several different activities to allow users to perform different operations. When a user clicks your application icon and responds to user operations, the main activity calls the oncreate () method to create an activity instance.

In the oncreate () method, you must operate on the program startup logic that will only be called once throughout the lifecycle. For example, you can define the user interface in the oncreate () method or initialize the content of some class variables.

For example, the following code shows you some basic settings for executing activities in the oncreate () method, such as declaring the user interface (defined in the XML layout file) and defining member variables, and configure the UI.

Textview mtextview; // member variable for Text View in the layout
 
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
 
// Set the user interface layout for this activity
// The layout file is defined in the project Res/layout/main_activity.xml File
Setcontentview (R. layout. main_activity );
 
// Initialize member textview so we can manipulate it later
Mtextview = (textview) findviewbyid (R. Id. text_message );
 
// Make sure we're re running on honeycomb or higher to use actionbar APIs
If (build. version. sdk_int> = build. version_codes.honeycomb ){
// For the main activity, make sure the app icon in the action bar
// Does not behave as a button
Actionbar = getactionbar ();
Actionbar. sethomebuttonenabled (false );
}
}

Note: In android2.0 (API 5 level) or later, use sdk_int to prevent the old operating system from executing new API functions, in earlier versions, the error "Running exception" will occur. Once the oncreate () method is executed, the onstart () method and onresume () method are executed quickly. Your activity will never stay in the created or started status. After the onstart () method is called, the activity will become visible, but the onresume () method will be quickly called, and the activity will remain in the resumed State until something happens, for example, you receive a call, navigate to another activity, or the screen is disabled.

 

In other subsequent content, you will learn the amazing use of other startup functions to restore your activity from the paused or stoped state. Note: The oncreate () method contains a parameter savedinstancestate, which will be discussed in the content of the reconstruction activity.

Figure 2: This is another example describing the activity lifecycle structure. It emphasizes the call sequence of the three main callback functions in creating an instance: oncreate (), onstart (), onresume (). Once the call order is complete, the activity will reach the resumed status that the user can interact with until the user selects another activity.


Destroy Activity

The first lifecycle function of an activity is oncreated, And the last lifecycle function is ondestroy (). The system calls the ondestroy () function as the last signal to completely destroy the activity from the memory. Most applications do not need to use this function, because local class references will be destroyed together with the activity, and some cleanup work is mainly in onpaused () and onstop. However, if your activity contains resources that are running on oncreated () or other continuously running resources in the background thread line, you should clear them in the ondestroy () method, to avoid Memory leakage.

@ Override
Public void ondestroy (){
Super. ondestroy (); // always call the superclass
 
// Stop method tracing that the activity started during oncreate ()
Android. OS. Debug. stopmethodtracing ();
}

In general, the system will call the ondestory () method only after calling the onpause () method and onstop () method. However, there is one exception, that is, in your oncreate () call the finish () method. In some cases, when your activity starts another activity as a temporary decided operation object, you may need to call finish () in the oncreate () method () in this case, the system directly calls the ondestroy () method instead of calling other life cycle functions.

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.