Introduction:

This is an extension to the Android Activity call stack analysis.

The main content

  • The typical life cycle
  • Life cycle in exceptional cases
  • Matching rule of IntentFilter

The specific content

As the first of the four components, Activity is the most frequently used component. Normally, apart from Windows, Dialogs, and Toasts, the only interface we see is really an Activity. Here we introduce some difficult concepts in the use of activities, including life cycle and startup mode and IntentFilter matching rule analysis.

Activity lifecycle comprehensive analysis

This section divides the Activity lifecycle into two parts, a typical (normal) lifecycle and an exception lifecycle. The lifecycle of an exception is when the Activity is reclaimed by the system or destroyed and rebuilt due to a change in the Configuration of the current device.

Life cycle analysis in a typical case
  • The Activity starts for the first time: onCreate->onStart->onResume.
  • The Activity switches to the background (the user starts a new Activity or switches to the desktop), onPause->onStop(if the new Activity has a transparent theme, the current Activity does not call onStop).
  • The Activity is revisible from the background to the foreground, onRestart->onStart->onResume.
  • The user exits the Activity, onPause->onStop->onDestroy.
  • The Activity is visible from onStart until onStop. An Activity can accept user interaction before onResume goes to onPause.
  • Before a new Activity can be started, the Activity at the top of the stack must be onPause. Therefore, time-consuming operations cannot be performed in onPause.
  • There is no time limit in onStop, and resource recovery and release can be placed in onDestroy.
Life cycle analysis in abnormal cases
System configuration changes cause the Activity to be destroyed and rebuilt

For example, if the Activity is in portrait, if the screen is suddenly rotated, the Activity will be destroyed and recreated due to a change in system configuration. In exceptional cases onSaveInstanceState is called before onStop to save the state. After the Activity is recreated, onRestoreInstanceState is called after onStart to restore the previously saved data.

The process for saving data: When the Activity is terminated unexpectedly, call onSaveInstanceState to save the data -> The Activity delegates the Window, which delegates a ViewGroup (probably a DecorView) to the top-level container above it. The top-level container then notifies all child elements to save the data. The onSaveInstanceState and onRestoreInstanceState methods are called only when an Activity terminates abnormally. Other cases do not trigger.

This is a delegation idea, similar to Android: View drawing process, event distribution, etc.

Low priority activities are reclaimed due to insufficient resource memory

There are three Activity priorities: Foreground – Visible not foreground – background, from high to low. If a process does not have four components, it will be quickly killed by the system. Therefore, background work is best placed in a service.

ConfigChanges generally uses three options:

  • The locale language changes.
  • The accessibility of the keyborardHidden keyboard changes, such as when the user calls up the keyboard.
  • Orientation Screen orientation changes.

The startup mode of the Activity

The Activity of the LaunchMode

Android uses a stack to manage activities. Refer to the Android Activity call stack for analysis.

The Activity of the Flags

These flags can set the startup mode and affect the running state of the Activity. Refer to the Android Activity call stack for analysis.

Matching rule of IntentFilter

How to invoke the Activity:

  • Displays a call that explicitly specifies the component information of the launched object, including the package name and class name
  • An implicit call requires that the Intent matches the IntentFilter set in the target component without specifying the component information.

Matching rules:

  • IntentFilter filters action, category, and Data.
  • Only an Intent that matches both the action category, category category, and data category can successfully launch the target Activity.
  • An Activity can have multiple intent-filters. An intent that matches any set of intent-filters can successfully start the Activity.
action

An action is a string, and a match is the same as the string of the action, case sensitive. An intent-filter can have multiple acitons, as long as the action in the intent is the same as any action. If no action is specified in the Intent, the match fails.

category

Category is a string. Intents can have no categories, but if you have several, each category must be the same as one of the categories in the intent-filter. System when startActivity and startActivityForResult will DEFAULT to Intent with android. The Intent. The category. This category, the DEFAULT So for the sake of our activity can receive call, must be in intent – filter with android. The intent. The category. The DEFAULT in this category.

data

The matching rules for data are the same as those for actions. If data is defined in an intent-filter, then the matching data must be defined in the intent. Intent-filter data syntax intent-filter data

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

Data in an Intent consists of two parts: a mimeType and a URI. MimeType refers to media types, such as image/ JPEG, audio/ MPEG4-generic, and video/. It can refer to different media types, such as pictures, texts, and videos.

URI structure:

 <scheme>://<host>:<port>/[<path>|<pathPrefix>|<pathPattern>]
Copy the code

Practical examples:

content://com.example.project:200/folder/subfolder/etc
http://www.baidu.com:80/search/info
Copy the code

Scheme: URI mode, such as HTTP, File, content, etc. The default value is file. Host: indicates the host name of the URI. Port: indicates the port number of the URI. Path, pathPattern, and pathPrefix: these three parameters describe path information. Path and pathPattern can represent complete path information. The pathPattern can contain wildcard character *, which indicates zero or more characters. PathPrefix indicates only the pathPrefix.

If the uri of the filtering rule is null, the default values are Content and file, so the scheme part of the URI must be content or file. When an Intent specifies data, the setDataAndType method must be called. SetData and setType clear the value of the other party. The same rules apply to Services and BroadcastReceivers, but it is best to use explicit calls for Services.

Implicit calls need to be noted:

  • When start the Activity by implicit call, didn’t find the corresponding Activity system will be thrown. Android content. ActivityNotFoundException exception, so you need to determine whether there is the Activity to be able to match our implicit Intent.
  • Use the PackageManager resloveActivity method or the Intent resloveActivity method.

Use MATCH_DEFAULT_ONLY above the second parameter, the meaning of the sign bit is the only match those in the intent – filter declared in the android. Intent. The category. The DEFAULT Activity in this category. Because if an Activity that does not contain the category is matched, an Activity that does not contain the category DEFAULT cannot accept an implicit Intent, causing startActivity to fail.

public abstract List<ResolveInfo> queryIntentActivityies(Intent intent,int flags);
public abstract ResolveInfo resloveActivity(Intent intent,int flags);
Copy the code
  • The following action and category are used to indicate that this is an entry Activity and will appear in the system’s list of applications, either without the other.
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Copy the code