An Activity is an application component that allows users to interact with a screen to perform actions such as making phone calls, taking photos, sending emails, or viewing maps. Each Activity gets a window to draw its user interface. Windows usually fill the screen, but can be smaller than the screen and float above other Windows.

directory

– Life cycle

– Boot mode

-onNewIntent

-Intent Filter

-onConfigurationChanged

– Data saving and recovery

-URL Scheme

-startActivityForResult

– Source code analysis

The life cycle

Before introducing the Activity lifecycle, let’s look at the four basic states of an Activity:

Active/Running: The Activity is Active. The Activity is visible at the top of the stack and can interact with users.

Paused: An Activity changes to Paused when it loses focus, is placed at the top of the stack by a new, non-full-screen Activity or by a transparent Activity. However, we need to understand that the Activity only loses the ability to interact with the user, all its state information and its member variables are still present, and can only be reclaimed by the system if the system is running out of memory.

Stopped: When an Activity is completely overwritten by another Activity, the overwritten Activity is in the Stopped state, where it is no longer visible but holds all of its state information and its member variables as Paused.

Killed: When an Activity is discarded by the system, it is Killed. The Activity switches between these four modes, and how it switches varies depending on the user. Now that you know about the four forms of an Activity, let’s talk about the Activity lifecycle.

onCreate

The first method in which an Activity is created, indicating that it is being created, where some of the initialization work is done (loading layout resources, initializing required data, etc.), and if there are time-consuming tasks that need to be performed by an asynchronous thread.

onStart

The Activity is being started, and in this state the Activity is loading other resources to show us that the user can’t yet see and interact with

onResume

After the Activity is created, the user can see the interface and interact with it

onPause

The Activity is being paused, and onStop() normally follows. You can store the data and stop the animation, but do not take too long to create a new Activity

onStop

When the Activity is about to stop, you can do some recycling, but it shouldn’t be too time-consuming

onDestory

The Activity is about to be destroyed, so it can do some work and recycle resources (Service, BroadCastReceiver, Map, Bitmap, etc.)

onRestart

This method is performed when the current Activity is being restarted and is no longer visible, such as when the user presses the Home button (locks the screen) or opens a new Activity and returns to the Activity

Think about the problem :(actually printing the execution order of the life cycle)

1. Start AActivity 2. Press the Home button to return to the Home screen. Press the Home button to return to the Home screen, and click the App again to return to AActivity 4.AActivity to start BActivity 5. Click the Back button in the BActivity to roll Back 6. Turn off the screen and light up 7. System configuration changes (vertical screen switching) 8. Activities with low priority are killed due to insufficient memory

Boot mode

standard

Standard mode is the default startup mode for Android. If you don’t set any Settings in your configuration file, this Activity is Standard mode. In this mode, you can have multiple instances of an Activity, and each time you start an Activity, A new Activity instance is created regardless of whether an instance of the Activity already exists in the task stack

singleTop

The SingleTop mode is very similar to the Standard mode. The main difference is that when a SingleTop mode Activity is already at the top of the task stack, no new instances are created when it is started. Now change the startup mode of FirstActivity in the configuration file to SingleTop. Our application has only one Activity, and FirstActivity is naturally at the top of the task stack.

SingleTask

The SingleTask mode has only one instance of an Activity in the same Task. If the Activity is already at the top of the stack, the system does not create a new instance of the Activity, as in singleTop mode. When an Activity already exists but is not at the top of the stack, the system moves the Activity to the top of the stack and pushes the Activity above it off the stack. Modify the program above, create a new SecondActivity, set FirstActivity to singleTask startup mode, and have it start SecondActivity, and SecondActivity start FirstActivity.

singleInstance

SingleInstance is also a singleton, but unlike singleTask, which is only a singleton in the task stack, the system can have multiple instances of singleTask Activity. A singleInstanceActivity has only one instance in the entire system. When a singleInstanceActivity is started, the system creates a new task stack that has only one Activity in it.

SingleInstance mode is not commonly used. If you set an Activity to SingleInstance mode, you will find that it will start slowly and switch poorly, affecting the user experience. It is often used among multiple applications, such as an Activity in a TV launcher. The Activity can be started in any case with a button on the remote control, and the Activity can be set to singleInstance mode. When the button is pressed to start the Activity in an application, press the back button after processing. It goes back to the app that started it before, and it doesn’t affect the user experience.

onNewIntent

Avoid multiple materialization of jump passes between activities

If you set the launchMode of your Activity to “singleTop” in androidmanifest.xml, or if you call startActivity(Intent), With the FLAG_ACTIVITY_SINGLE_TOP flag set, when the Activity is started again, if it still exists in the Activity stack and is at the top of the stack, it will not be recreated. Instead, the original instance will be used. OnNewIntent (Intent) will be called, and other methods in the subsequent lifecycle can use the new Intent parameter passed in. (That is, other methods can use the updated Intent parameter.)

OnNewIntent () -> onRestart() -> onStart() -> onResume()

Note that if you do not call setIntent(Intent) to update the Intent, then getIntent() will return the original value.

IntentFilter

Intents filter. Activities communicate with each other through intEnts. Intent-filters are used to register activities, services, and Broadcast receivers so that Android knows which application (or component) can be used to respond to an intent request so that it can perform that action on a piece of data. To register an application component as an intent handler, add an intent-fillter tag to its component’s manifest node.

1. Action Test

An intent object can specify only one action, and an element should contain at least one action; otherwise, any intent request cannot match that action.

An intent action must match an Actiong in the intent-filter to pass.

If the intent does not specify an action and the action list in the intent-filter is not empty, the intent passes.

2. Category Test (category)

Intent-filter must contain all categories defined in the Intent being parsed. An IntentFilter without a specific type can only be matched with an Intent without a specific type. Additional declarations in an IntentFilter do not result in a failed match. An important point is that if an intent is an implicit intent, Android gives CATEGORY_DEFAULT by default. So if the intent filter. There are no android intent. The category. This category, the DEFAULT matching test will fail, in other words, must be enclosed in this category.

3: Data testing

Data consists of two parts, one is a data type and the other is a URI. Each URI contains four attribute parameters (Scheme,host, port, path), as follows: Scheme ://host:port/path, where the URI data type and scheme of the Inteat request set by setData() must be the same as those specified in IntentFilter. If the IntentFilter also specifies authority or path, they also need to match to pass the test. Intent Filter and Intent work together to implement the messenger function between the four components of Android.

URL_Scheme, which uses data, will be described later

onConfigurationChanged

In some special cases, you may want to avoid restarting your activity when one or more configurations change. You can do this by setting the Android :configChanges property in the manifest. You can declare any Configuration changes your activity can handle here, and instead of restarting the activity when those Configuration changes are made, the activity’s onConfigurationChanged(Resources.Configuration) method is called. If the changed configuration contains configurations that you can’t handle (not declared in Android :configChanges), your activity should still be restarted. OnConfigurationChanged (Resources.Configuration) will not be called.

Second: the android: configChanges = “” value can be used in: the rid_device_info_keyboard | MCC | newsun focus | locale | touchscreen | keyboardHidden | navigation | orientation… The Configuration class contains a variety of information, such as system font size, orientation, input device type, and so on. Such as: the android: configChanges = “orientation” | the rid_device_info_keyboard | keyboardHidden”

Data preservation and recovery

Data save and Restore activities (onSaveInstanceState and onRestoreInstanceState) are used when the system configuration changes.

URL_Scheme

Scheme is an in-page jump protocol, which is a very good implementation mechanism. By defining your own Scheme protocol, you can easily jump to each page in app. Through scheme protocol, the server can customize to tell App which page to jump to, customize to jump to page through notification bar message, and jump to page through H5 page, etc.

A complete Scheme protocol format consists of Scheme, userInfo, host, port, Path, Query, and fragment. The structure is as follows:

scheme://userInfo@host:port/path? query#fragment

startActivityForResult

StartActivityForResult is used when the Activity needs to upload a value to the Activity when it finishes. For example, open the album, select a photo, take a photo, close the page and return a parameter to open our page.

Use startActivityForResult(Intent Intent, int requestCode) to open a new Activity. Before a new Activity closes, it needs to return data to the previous Activity using the system-provided setResult(int resultCode, Intent data). In the previous Activity, the onActivityResult(int requestCode, int resultCode, Intent Data) method accepts the returned value

Source code analysis

Look forward to the following articles