The Activity lifecycle and startup mode

The typical life cycle

  1. OnCreate () => onStart() => onResume()

  2. Start a new Activity & Return to the desktop: onPause() => onStop(), when using a transparent theme, the Activity does not go onStop()

  3. The user returns to the original Activity: onReStart() => onStart() => onResume()

  4. Back button rollback: onPause() => onStop() => onDestroy()

  5. The Activity is reclaimed by the system

  6. The onCreate() and OnDestroy() pairings are called only once, the onResume() and onPause() pairings are called multiple times with the user action and the on-off screen action, as are onStart() and onStop().

What is the difference between onStart() and onStop() and onResume() and onPause()?

The onStart() group is related to the background, while the onResume() group is related to the foreground display. Since the life cycles of these two groups are directly related to the display, time-consuming operations on onPause() and onResume() are not recommended.

Note: ActivityA can only be displayed after ActivityB A’s onPause

Life cycle in exceptional cases

  1. Resource related system configuration changes cause the Activity to be rebuilt

    In this case, for example, when the device switches from landscape to portrait, the system will apply image resources in different situations, such as Drawable-landscape. By default, the system will destroy the current Activity and rebuild the Activity.

    OnPause (),onStop(), and onDestroy() are all executed when the rebuild occurs. Since the rebuild is terminated by an exception,onSaveInstanceState() is called to save the current state. OnSaveInstanceState () is called in onSto The onRestoreInstanceState() is called after onStart(). The Bundle communicates data between the two methods. By default, the system does something for us,on SaveInstanceState() and onRestoreInstanceState() by default help us save and restore data such as the scroll view’s sliding position, the input box’s entered content, and so on. For details and procedures, see the Android source code.

  2. Low-priority activities were killed due to insufficient resource memory

    Prioritizing the Activity

    1. The Activity being displayed in the foreground
    2. Visible non-foreground activities, such as an Activity that pops up a full-screen Dialog
    3. Background activities

Similarly, being killed can be rebuilt via onSaveInstanceState() and onRestoreInstanceState().

I want to avoid reconstructing my Activity when some system configuration changes. How do I do that?

You can specify Activity android:configChanges = “” You can query the corresponding parameter list and specify that the corresponding scenario does not rebuild the Activity, which can be handled in the onConfigurationChanged() callback of the Activity.

Android startup mode

  1. Standard: indicates the standard mode

    Each time you use this pattern, a new instance is created that executes the full typical lifecycle and runs in the task stack of the Activity that started it.

    Why can’t you start an Activity in standard mode using applicatioContext?

    Because the application does not have its own task stack, it cannot store the instance of the newly started Activity. It usually returns the NEW_TASK FLAG. In fact, this step uses the SingleTask method to start the new Activity and create a new task stack for it.

  2. SingleTop: stack top reuse

    If the new Activity is already at the top of the stack, the Activity will not be rebuilt, but onNewIntent will be triggered. We can use this method to get information. The onCreate() and onStart() of the Activity will not be called again if the Activity is not started At the top of the stack, it will still rebuild.

  3. SingleTask: in-stack reuse

    If the Activit does not exist, an instance of the Activity will be created, and the system will confirm that the Activity requires the task stack to exist, or create a task stack if it does not. This is similar to singleTop, but the Activity will not be rebuilt as long as it exists in the stack, and onCreate() and onStart() will not be regenerated. The information can be retrieved in onNewIntent, and the instance will be pushed onto the stack if it does not exist If there is Activity ABCD in stack 1, and B is started as a SingleTask, and the task stack required by B is also stack 1, then the system will cut the current foreground Activity back to B and remove the CD, so the final result is AB.

  4. SingleInstance: single-instance mode

    The Activity created in this mode only exists in a separate task stack. Once created, subsequent requests do not create new Activity objects unless the stack is destroyed.

Matching rule of IntentFilter

There are two ways to start an Activity: display and implicit.

Display startup specifies the startup object by specifying the package name and class name of the startup object.

IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter: IntentFilter IntentFilter filter information includes Action,category, and data.

<intent-filter>
    <action android:name="com.spica.a"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="text/plain"/>
</intent-filter>
Copy the code

To match the filter information list, you need to match all actions, categories, and data at the same time.

You can have multiple actions, categories, and data.

An Activity can have multiple sets of intent-filters, and the Activity can be started as long as the intent matches any one of these sets.

Action matching rule

Action is a string of predefined actions. We can define our own actions.

The Action in the intent must be exactly the same as the Action in the filtering rule. If multiple actions are specified in the filtering rule, only one Action needs to be matched.

The intent Action must exist; otherwise, the match fails.

Acyion is strictly case sensitive.

Matching rules for categories

So a Category is also a string, so there are predefined categories, and you can define your own;

If the intent contains a Category, each Category must be the same as one of the categories in the filtering rule, no matter how many categories there are.

Intents can have no Category; calling startActivity() or startActivityForResult() adds one to the intent by default “Android. Intent. The category. The DEFAULT”, therefore, for the Activity can be received call, you must add “android. Intent. The category. The DEFAULT” rules of the filter;

Data matching rules

If data is defined in the filtering rule, you need to define matching data in the intent.

Example syntax for data:

Data consists of mineType and URL. MineType refers to media types, such as image/ JPEG,audio/ MPEG4-generic,video/*, etc.

Format of URI:

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

Specific examples:

content://com.example.project:200/folder/subfolder/etc

https://www.baidu.com:80/search/info

Scheme: the pattern of urIs, such as file, Scheme, content, etc. If the URL does not specify Scheme, the URI is invalid.

Host: the Host name of the URI, such as www.google.com. If Host is not specified, other parameters in the entire URL are invalid. You could also say that the entire URL is invalid;

Port: indicates the Port number. The Port number is valid only if Scheme and Host are specified.

Path, pathPrefix pathPattern: used to describe the Path information, said the Path the full Path information, pathPrefix also said the complete Path information, but it can contain the wildcard “”,” “is used to represent zero or more characters, as a result of the regular expression specification, if it is to express the real word String,” * “should be written as “\\ * “,”\” should be written as “\\\\”,pathPatter represents prefix information;

The matching rule for data is similar to that of an Action. It also requires that the intent must contain data and that the data matches exactly one of the data in the filtering rule.

Such as

<intent-filter>
<data android:mineType = "image/*"/>.</intent-filter>
Copy the code

Intent_data = “image/*”; intent_data = “image/*”;

The default value is “Content” or “file”. The Scheme of all intent urls must also be “content” or “file”.

To match the rules here we can write:

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

To specify a complete data for an intent, you must use setDataAndType(). If you call setData and setType separately, the two methods cancel each other out. SetData will set mineType to null,setType will set URI to null