Chapter One: Life cycle and startup mode of activity

Source: Internet
Author: User

Activity is the top of the four components of Android , so it is important to focus on, especially the startup mode, the registration information in androidmanifest.xml

  A Life cycle of activity

  1.1.1 Life cycle under normal conditions

Description

(1) For a specific activity, the first boot sequence: Oncreate->onstart->onresume.

(2) When the user opens a new activity or switches to the desktop, the callback is as follows: Onpause->onstop

(3) When returning to the original activity, the callback is as follows: Onrestart->onstart->onresume.

(4) When the user presses back to return Yes, the callback is as follows: Onpause->onstop->ondestroy

*onstart and OnStop are callbacks from the perspective of whether the activity is courseware or not, and the two onresume and OnPause are callbacks from the perspective of whether the activity is located in the foreground.

The launch of *activity involves Instrumention,activitythread and Activitymanagerservice (AMS).

The request to initiate the activity is handled by instrumention, which then sends a request through binder to AMS, which maintains the activitystack and is responsible for the state synchronization of the activity within the stack. AMS uses Activitythread to synchronize the state of the activity to complete the invocation of the life cycle method

(Android official documentation for OnPause interpretation, reminders can no longer be onpause in the heavyweight operation, because must onpause after the execution of new activity to resume)

  Life cycle analysis in the case of 1.1.2 anomalies

(1) configuration file changes cause exception

Note: When the system configuration is changed, the activity will be destroyed, its onpause,onstop,ondestroy will be called, and because the activity is terminated in exceptional circumstances, The system will call Onsaveinsatncestate to save the state of the current activity.

(Onsaveinsatncestate is not normally called)

An exception occurs, the system's workflow: The activity invokes onsaveinsatncestate to save the data, then the activity delegates the window to save the data, and the window then delegates the top-level container above it to save the data.

The top-level container is a viewgroup, which in general is likely to be decorview. The last top-level container then notifies its child elements to save the data (delegate idea, upper layer delegate lower layer, container delegate child element to handle event)

(2) Insufficient resource memory causes Low priority activity to be killed

Workaround: Put the background work into service to ensure that the process has a certain priority, will not let the system kill

* When the configuration file changes, the activity is re-created by assigning the configchanges attribute to the activity. (Hey androidmainfest.xml add a statement)

* Three commonly used options:

      1. Locale: The local location of the device has changed, generally referred to as switching the system language
      2. Orientation: The screen orientation has changed, this is the most common
      3. Keyboardhidden: The accessibility of the keyboard has changed, such as when the user has paged the keyboard

  1.2 Activity's Startup mode

The Launchmode of 1.2.1 activity

      1. Standard: Normal mode, each time an activity is started, a new instance will be built, regardless of whether the instance already exists
      2. Singletop: Stack top multiplexing mode, if the new activity is already at the top of the task station's stack, then the activity will not be recreated, and its Onnewintent method will be called, and the parameters of this method can be used to fetch the information of the current request.
      3. Singletask: In-stack multiplexing mode, which is a single-instance mode in which the activity is not recreated as long as it exists in a stack, and as with Singletop, the system calls its onnewintent
      4. Singleintance: Single-instance mode, a strengthened Singletask mode, in addition to having all the features of the Singletask mode, the activity of this mode can only be isolated in the task stack, due to the characteristics of the reuse in the stack, Subsequent requests do not create new activity unless the task stack is destroyed by the system

    

*activity the required task stack, with taskaffinity (task dependency), this parameter identifies the name of the task stack required by the activity, by default, all activity requires the name of the task stack to be the app's package name .

* The Taskaffinity property can be specified separately for each activity, with different names representing different task stacks android:taskaffinity= "property value is String"

* Task stack is divided into foreground task stack and background task stack

* There are two ways to specify the startup mode for activity:

      1. Through Android:launchmode= "Singletask" in Androidmanifest.xml
      2. Specify the startup mode for activity by setting the flag bit in intent

Chestnut: Intent.addflags (intent.flag_activity_new_task);

* The first priority is higher than the second.

    1.2.2 Activity's Flags

List the commonly used:

        1. Flag_activity_new_task: Specify "Singletask" startup mode for ACTIVITY
        2. Flag_activity_single_top: Specify "singletop" startup mode
        3. Flag_activity_clear_top: All ACTIVITY above it in the same task stack is out of stack
        4. Flag_activit_exclude_from_recents: Activity with this tag does not appear in the list of historical activity

    

    Matching rules for 1.3 intentfilter

There are two types of activity initiation: explicit invocation and implicit invocation. Use implicit invocation as much as possible

Explicit invocation:

You need to explicitly specify the component information for the object being launched, including the package name and class name

Implicit invocation: Requires intent to match the filtering information set in the Intentfilter of the target component, and the filtering information in the Intentfilter is Action,category,data

No need to explicitly specify component information

      

* A intent Match action type, category type, data type is exactly match to start target activity, one activity can have multiple intent-filter, A intent can successfully initiate a corresponding activity as long as it matches any set of Intent-filter

       Matching rules

        1.  Match rule for action

The action match requires that the action in intent exist and must be the same as one of the actions in the filter rule.

2. match rules for category

There can be no category in intent, and if so, each one needs to be the same as any category in the matching filter rule.

*startactivity or Startactivityforresult will default to intent plus the category "Android.intent.category.DEFAULT".

3. matching rules for data

Similar to action, if data is defined in the filter rule, then the intent must also define a matching data

Data syntax: (consists of two parts, mimetype refers to the media type, and URI, structure:<scheme>://

<data android:scheme= "string"

Android:host= "string"

Port/path/pathpattern/pathprefix/mimetype= "string" >

* Chestnuts: Set <data android:mimetype= "image/*" > Match tag in <intent-filter>, then MimeType attribute in intent must think "image/*" to match.

We recommend using Intent.setdatandtype, if you use SetData and Settype alone, you will clear each other's values

Matching chestnuts:

      

1 New Intent (""); 2 intent.addcategory (""); 3 Intent.setdataandtype (Uri.parse (data), type); 4

    

Chapter One: Life cycle and startup mode of activity

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.