Android Activity Summary

Source: Internet
Author: User

1.Activity Introduction

Activity is a very important user interface for Android (one of the four components) and is visible, primarily as an interface between the user and the application. You can put a lot of controls in each activity, so you can also think of activity as a container for controls.

2.Activity life cycle


-OnCreate: Primarily performs initialization work, such as loading the layout interface with Setcontentview
-Onstart:activity from invisible to visible, but not in foreground, no interaction with user
-Onresume: Make activity at the top of the return stack and appear in the foreground to interact with the user
-onrestart:activity reboot, visible from invisible
-OnPause: Indicates that activity is stopping and onstop will normally be called shortly thereafter. However, there are special cases where the onresume is also called if the current activity needs to be brandished quickly. The implementation of this method must be fast, otherwise it will affect the use of the top of the activity of the stack.
-Onstop:activity from partially invisible to completely invisible, and can do some heavy-duty recycling, also can not be time-consuming.
-Ondestory: Destroys activity and frees resources.

OnCreate and Ondestory are the full life cycle, OnStart and onstop for the visible life cycle, Onresume and onpause for the foreground life cycle.

Watch out.

    • When the user opens a new activity or switches back to the desktop, the callback is onpause->onstop, but if the activity uses a transparent theme, the OnStop will not be recalled.
    • In general, activity is not visible and Onrestart is called.
    • OnStart and OnStop control activity transitions between visible and invisible states, Onresume and onpause control activity transitions between foreground or non-foreground.
    • When Activity A->activity B, the order of callbacks is OnPause (A)->oncreate (b)->onstart (b)->onstop (a), so it is not possible to do heavyweight operations on OnPause.
    • If the application is in the stopped state for a long time and the system memory is extremely tense, the system will recycle the activity, and the system will callback the Onsaveinstancestate method to save the data bundle of the application before recycling. When the activity is recreated, the saved bundle data is passed to the Onrestoresaveinstancestate method and the OnCreate method, which is the bundle in the OnCreate method The source of the Savedinstancestate parameter (the bundle parameter of the onrestoreinstancestate is also passed to the OnCreate method, and you can choose to do a data restore in the OnCreate method).

The Onsaveinstancestate method and the Onrestoreinstancestate method "not necessarily" are called in pairs.

Onsaveinstancestate's invocation follows an important principle that when your activity is destroyed by the system "without your permission", the onsaveinstancestate is called by the system, which is the responsibility of the system, Because it has to provide an opportunity for you to save your data.

Onrestoreinstancestate is invoked only if the activity is "indeed" destroyed by the system, and if it is simply stuck in the case where there is such a possibility, then the method will not be called, for example, when the activity is being displayed. The user presses the home button back to the main interface, then the user then returns to the activity, in which case the activity is generally not destroyed by the system for memory reasons, so the activity's Onrestoreinstancestate method will not be executed.

3. Abnormal life cycle

1. Changes in resource-related system configuration cause activity to be killed and rebuilt

Resource-related system configuration changes, for example, a chestnut. The current activity in the vertical screen when suddenly turned into a horizontal screen, the system configuration has changed, the activity will be destroyed and rebuilt, its onpause, OnStop, Ondestory will be called. The system calls Onsaveinstancestate to save the current activity state because it is actually terminated in an abnormal situation. This method is not a fixed sequence relationship with OnPause prior to OnStop. When the activity is rebuilt, the system will pass the bundles saved by Onsaveinstancestate as objects to the Onrestoreinstancestate and OnCreate methods.

Note:

    • Each view in the source of the view has Onsaveinstancestate and onrestoreinstancestate two methods.
    • The receive location can be onrestoreinstancestate and OnCreate method, the difference is: onrestoreinstancestate if called, the parameter bundle must be a value, In OnCreate, you need to determine if the parameter is null.
    • Onsaveinstancestate is only called when the activity is about to be destroyed and has the opportunity to re-display, and will not be called during the normal destruction activity life cycle, such as rotating the screen, pressing the home key, initiating a new activity, and so on.

2. Low-priority acitvity are killed due to insufficient resource memory

Activity priority

    1. Front desk activity--is interacting with the user, with the highest priority
    2. Visible but non-foreground activity--activity pop-up dialogs cause activity to be visible but not interactive
    3. Background activity--has been suspended activity, the lowest priority

The system is running out of memory, killing the activity in the order above, and storing and recovering the data through both Onsaveinstancestate and Onrestoreinstancestate methods.

Methods that do not allow activity to be recreated

There is a lot of content in the system configuration, and when a change is made, we don't want the activity to recreate the Configchanges attribute that can be assigned to activity in Androidmainfest. Like what

 android:configChanges="orientation"1

Configchanges properties are very numerous, refer to official documents

There are three commonly used locales, orientation and keyboardhidden.

4.Activity task stack and four startup modes

In-app activity is managed by task stack tasks, where activity from a task can come from different applications, and the activity of the same application may not be in the same task. By default, the task stack manages activity based on the last-in-first-out principle of the stack, but activity can set some "privileges" to break the default rules, primarily through the properties of Android in the Androidmanifest file: Launchmode or set by intent flag.

Standard: The default startup mode, in which a new activity is generated and the activity instance is pressed into the stack (regardless of whether the activity already exists in the task stack, it is in the new operation). For example: The order in the stack is a B c D, at this time D through intent jump to a, then the stack structure becomes a B c D A, click the return button to display the order is D C B A, in turn destroy.

Singletop: In singletop mode, if the current Activity D is at the top of the stack and then jumps to its own activity (that is, d) by intent, then a new D instance (walk Onnewintent ()) is not recreated. So the structure of the stack is still a B c D, if jump to B, then because B is not at the top of the stack, so a new B instance is created and pressed into the stack, the structure becomes a B c D b. Application example: Three push, point in is an activity.

Singletask: In Singletask mode, there can be only one instance of the task stack that corresponds to the activity. For example, the structure of the stack is now a B C D, at which point D jumps through intent to B (Walk Onnewintent ()), then the structure of the stack becomes: a B. C and D are destroyed by the stack eject, which means that instances above B are destroyed. If the system already exists an instance, the system will send the request to this instance, but at this time, the system will no longer invoke the OnCreate method that normally we process the request data, but call the Onnewintent method. Usually applied to the home page, the home must be at the bottom of the stack, but also at the bottom of the stack.

In singleinstance:singleinstance mode, the open activity is pushed into a new task stack. For example: The structure of the task Stack 1 is: a B c,c through intent jump to D (start mode of D is singleinstance), then a new task stack 2, the structure of the stack 1 is still a B C, and the structure of the stack 2 is D, when the screen shows D, After D jumps through intent to D, Stack 2 does not press into the new D, so the situation in the 2 stacks does not change. If d jump to C, then according to the C corresponding start mode in the stack 1 to do the corresponding operation, c if the standard, then D jump to C, the structure of the stack 1 is a B c C, at this time click the Back button, or C, the structure of the stack 1 into a B C, and will not go back to D.

Intent Flag Boot Mode

(1) Intent.flag_activity_new_task: Use a new TASK to initiate activity, which is typically used to start the activity in the service because there is no activity stack in the service.

(2) Intent.flag_activity_single_top: Similar to andoid:launchmode= "Singletop"

(3) Intent.flag_activity_clear_top: Similar to andoid:launchmode= "Singletask"

(4) Intent.flag_activity_no_history: When the activity is started using this mode, the activity disappears and is not persisted in the task stack when the activity initiates other activity. For example a B, starting c,c in this mode in B and then starting D, the current task stack becomes a B d.

Empty the task stack

(1) Cleartaskonlaunch: Each time the activity is returned, all activity on top of the activity is cleared. This property allows the task to have only one activity at a time when it is initialized.

(2) Finishontasklaunch:cleartaskonlaunch acts on other activity, while finishontasklaunch acts on himself. With this property, when you leave the task where the activity is located, the activity will be erased when the user returns. [I don't see how this works yet]

(3) Alwaysretaintaskstate: If this property of activity is set to true, then the task of the activity will not accept any cleanup command, keep the current task state, equivalent to the task of "Death gold".

Matching rules for 5.IntentFilter

    • (1) Intentfilter filter information in the action, category, data, in order to match the filter list, you need to match the filter list of the action, category, data information, otherwise the match failed. A filter list of action, category, data can have multiple, all action, category, data constitute a different category, the same category of information together to constrain the current category of matching process. Only one intent match the action, category, and data categories to match exactly, and only an exact match to successfully start the target activity. In addition, there can be multiple intent-filter in an activity, and a intent can successfully initiate the corresponding activity as long as it matches any set of Intenf-filter.



      <category android:name="com.ryg.category.c" /><category android:name="com.ryg.category.d" /><category android:name="android.intent.category.DEFAULT" /><data android:mimeType="text/plain" />

    • (2) Action matching rule

The action match is case-sensitive as long as the action in intent can match any action in the filter rule.

    • (3) Category matching rules

Intent if there is a category then all category must be the same category as the filter rule, if there is no category then the default category, That is Android.intent.category.DEFAULT, so in order for the activity to receive implicit calls, the configuration of multiple category must be accompanied by the default category.

    • (4) Data matching rules

The structure of data is complex and the syntax is roughly as follows:

<data android:scheme="string"android:host="string"android:port="string"android:path="string"android:pathPattern="string"android:pathPrefix="string"android:mimeType="string" />

Consists primarily of mimetype and URIs, where mimetype represents the media type, and the URI structure is complex, roughly as follows:

://:/[]| []| [Pathpattern]

such as Content://com.example.project:200/folder/subfolder/etc

Scheme, host, and port each represent the mode, hostname, and port number of the URI, where the URI is invalid if scheme or host is unspecified.

Path, Pathpattern, and pathprefix are all paths information, where path represents the complete path information, Pathprefix represents the path prefix information, Pathpattern represents the complete path, but it contains a wildcard character (*).

Data matching rules: The intent must contain data, and the data can match exactly one data in the filter rule.

URI has the default scheme!

If the mimetype in the filter rule is specified as image/ or text/ , the default scheme is content and file! even if no uri,uri is specified in the filter rule. If scheme is specified in the filter rules, then it is not the default scheme.

//URI有默认值<intent-filter>    <data android:mimeType="image/*"/>    ...</intent-filter>//URI默认值被覆盖<intent-filter>    <data android:mimeType="image/*" android:scheme="http" .../>    ...</intent-filter>

If you want to specify the full data for intent, you must call the Setdataandtype Method!

You cannot call SetData first and then call Settype, because these two methods will clear each other's values.

intent.setDataAndType(Uri.parse("file://abc"), "image/png");

The following two ways of writing the data are the same:

<intent-filter>    <data android:scheme="file" android:host="www.github.com"/></intent-filter><intent-filter>    <data android:scheme="file"/>    <data android:host="www.github.com"/></intent-filter>

How can we tell if there is activity that matches our implicit intent?

(1) Packagemanager Resolveactivity method or Intent Resolveactivity method: Returns null if not found

(2) Packagemanager Queryintentactivities Method: It returns all activity information that was successfully matched

For components such as service and Broadcastreceiver, Packagemanager also provides a similar way to obtain component information that is successfully matched, such as queryintentservices, Querybroadcastreceivers and Other methods

One type of action and category are important, and they are used together to indicate that this is an entry activity and that it will appear in the list of applications in the system.

<intent-filter>    <action android:name="android.intent.action.MAIN" />    <category android:name="android.intent.category.LAUNCHER" /></intent-filter>

The composition of the 6.Activity

The composition of the activity is not as simple as an Activity object plus a layout file, and there are two layers between the activity and the developer. The view is actually set to a window class, with a decorview in the window class, which is the top-level view of the entire Windows. The developer-set layout is set to the Decorview mcontentparent layout.

This way, the user interface is added to the system layout, and the system layout sets up the title bar area for us.

Activity->PhoneWindow->DecorView->DefaultLayout->ViewGroup:mContentParent->用户自己的xml布局

Android Activity Summary

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.