[01] Activity of four components

Source: Internet
Author: User

First, Activity's Basic ConceptsConcept: Represents a window interface that can be used for task interaction. Multiple levels of view and ViewGroup can be included.Create: Inherits an activity or subclass that it already exists. You must implement the OnCreate () method, calling Setcontentview () to load the layout of user interaction. and configure a <activity> node in Androidmanifest.xml <application> to indicate the Android:name attribute.Start an activity: (1) Start the main activity. The manifest file configures the category "MAIN" Action and "LAUNCHER". (2) Start normal activity using intent. The general invocation of StartActivity () consists of two ways, display and implicit. Display refers to the name of the activity that intent indicates to start, and it is implicit that you do not specify a specific activity, describe only the action of intent, or add data to allow the system to filter Intent-filter to select the most appropriate activity. (3) A result needs to be obtained from the activated activity. Start the activity with Startactivityforresult (), indicate the intent and requestcode, and then implement the Onactivityresult () callback interface to get the result after the return. Onactivityresult () to determine whether the ResultCode is a successful return, by Requestcode to determine whether the response is the specified request. Ii. life cycle management of activityWhy manage the lifecycle, activity is the main body of the application, is the first window of the user experience (see), of course, it is important. The principle here usually has the following points to follow:
    1. When using your app, it will not cause the program to crash (call, Alarm, other programs, rotate the screen) because of the screen switch;
    2. Do not waste valuable system resources when users are not using or need certain components;
    3. will not cause some critical user progress loss due to leaving the current app for some time.
    4. ... ... ...
How to manage it, by implementing callback methods for each stage of activity life. Here is a callback method for the activity and for the Life state diagram: Only three states are static, activity can remain in these three states for a long time, other states will quickly switch off, stay for a short time.
    • Resumed: Also known as the runing State, activity is at the forefront, capturing focus and interacting with it.
    • Paused:activity is partially obscured by another activity (the other activity is translucent or only covers a portion of the screen, in summary the activity of the Paused state is visible but not operational to the user)
    • Stoped:activity is completely hidden, not visible to the user,
common operations for each callback method: OnCreate (): Set layout, define member variables, configure UI elements, etc. (do as little as possible, or wait for the activity to display for too long) Onrestart (): The activity has not been restarted since the last stopped. OnStart (): Continue to maintain the resources you need to display in your activity, manage them as appropriate, or register for a broadcast (not long) onresume (): What you need to release in OnPause (), what to restore here OnPause () : Stop animations or other CPU-wasting operations and release resources such as Sencor, camera, etc. To avoid entering the next activity for too long, this callback function try not to do CPU time-consuming operationsFor specific reasons, see the callback execution process below activitya-->b. OnStop (): Here you can store the required data in persistent storage (File, DB, etc.) OnDestroy (): Frees up all the resources that are not needed for the current activity, such as child threads, cursor, and so on.two other life-cycle-like approaches: Onsaveinstancestate (): Once activity enters the non-foreground process, leaving the resumed state (runing state), it is possible that the program process is killed in extreme cases due to insufficient system memory. So if we need to save some of the information we need on the activity UI so that we can reuse that information the next time we create the activity (in the bundle), we'll be able to put the required value into the bundle in Onsaveinstancestate (). Onrestoreinstancestate (): This callback is implemented if the activated activity was last unexpectedly forcibly closed and the last usage progress value needs to be restored. FromActivitya start Activitybcallback function Execution Flow: (The following discussion launchmode of activity is standard)Press the back key from Activityb to return Activitya:rotating the screen Activitya: If you do not want to re-create the activity, you can configure <activity> in Androidmanifest.xml with the following configuration. Android:configchanges= "Orientation|screensize|keyboardhidden" iii. Four kinds of activation modes and application scenarios of activitysettings for startup mode: <activity android:name= ". Mainactivity "android:launchmode= "Standard"/> can also be used in startactivity () to intent and corresponding flag bit flag_activity_***.Four startup modes:
    • Standard

Standard startup mode, each time an activity is activated, an instance of the activity ( even yourself) is created and placed in the task stack.

    • Singletop

If there is an instance of the activity at the top of the task stack, the instance is reused. ( only at the top of the stack, onnewintent () will be adjusted when reused)

    • Singletask

The entire task stack can have only one instance of the activity, and if an instance of the activity already exists in the stack, the instance is reused ( onnewintent () of the instance is invoked). When reused, the instance is returned to the top of the stack, so the instances above it will be removed from the stack. If the instance does not exist in the stack, a new instance is created to be put into the stack.

    • SingleInstance

Create the activity instance in a new stack and have multiple apps share the activity instance in the stack. Once an instance of the activity in the schema is present on a stack, any application that activates the activity will reuse the instance in that stack, with the effect of sharing an app with multiple applications, regardless of who activates the activity into the same application

Application Scenarios: The default value is standard, specific analysis, but to ensure that activity usability testing, including the startup process, and other activity interaction process, return key Home key, etc. Iv. Other1, about <activity> in the Androidmanifest.xml configuration attribute in Chinese introduction reference this blogger's blog: http://www.cnblogs.com/popapa/p/android_ Activity-element.html 2, Activity popup toast or dialog does not causeChanges in the life cycle.

[01] Activity of four components

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.