This paper briefly introduces the basic concept of activity control in Android development

Activity is the most basic module, which is generally called "activity". In an application, an activity is usually a separate screen. Simply understand, an activity represents the screen that a user can see. It is mainly used to handle the overall work of the application, such as listening to system events, displaying the specified view for the user, starting other activities, etc. All application activities inherit from android.app.activity class, which is the basic class provided by Android. Other activities inherit the parent class and realize various functions through the methods of the parent class.

The activity life cycle diagram is as follows:

In Android, activity has four basic states:

1. Active / running after a new activity is started and put into the stack, it is at the front of the screen and at the top of the stack. At this time, it is in a visible and interactive activation state.

2. Paused the state when the activity is overwritten by another transparent or dialog style activity. At this time, it is still connected to the window manager, and the system continues to maintain its internal state, so it is still visible, but it has lost its focus, so it cannot interact with users.

3. Stopped when an activity is covered by another activity, loses focus and is not visible.

4. The killed activity is in the killed state when it is killed, recycled or not started by the system.

When an activity instance is created, destroyed or another activity is started, it transitions between these four states. The occurrence of this transition depends on the action of the user program.

As shown above, Android programmers can decide the "life" of an activity, but not its "death", that is, programmers can start an activity, but can't manually "end" an activity. When you call the activity. Finish () method, the result is the same as when the user presses the back key: tell the activity manager that the activity instance has completed the corresponding work and can be "recycled". Then, the activity manager activates the activity at the second layer of the stack and re enters the stack. At the same time, the original activity is pushed into the second layer of the stack and changes from active state to paused state. For example, if activity2 is started from activity1, activity2 is currently at the top of the stack and activity1 is at the second level. When we call activity2. Finish() method, the activity manager reactivates activity1 and merges it into the stack. Activity2 transitions from active state to stopped state, activity1. Onactivityresult (int requestcode, int resultcode, intent data) Method is executed, and the data returned by activity2 is returned to activity1 through the data parameter.

When non user behavior makes the activity invisible, such as when the phone suddenly comes==

In Android, there are four activation modes, namely:

These startup modes can be set in the function manifest file androidmanifest.xml, and the launchmode attribute in.

There are also some flags in the relevant code. For example, if we want to enable only one instance, we can use intent.flag_ ACTIVITY_ REORDER_ TO_ Front flag, which indicates that if the activity has been started, no new activity will be generated, but only the activity instance will be added to the top of the stack.

The loading mode of an activity is interactively controlled by the flag set in the intent object that starts the activity and the attribute value of the activity element in the manifest file.

Here are some features that affect the loading mode

The core intent flags are:

The core features are:

Differences between four loading modes

Differences between tasks

Generally, the target task of the activity of "standard" and "singletop" is in the same task as the sender of the received intent, which is equivalent to the same task as the one who calls it.

Unless intent includes the parameter flag_ ACTIVITY_ NEW_ TASK。 If flag is provided_ ACTIVITY_ NEW_ The task parameter will be started in other tasks.

"Singletask" and "singleinstance" always take the activity to be started as the root element of a task, and they will not be started into another task.

Allow multiple instances

"Standard" and "singletop" can be instantiated multiple times and can exist in different tasks; When instantiating, a task can include multiple instances of an activity;

"Singletask" and "singleinstance" restrict the generation of only one instance and are the root element of the task.

Singletop requires that if there is already an instance of the activity to be created at the top of the stack when creating the intent, the intent will be sent to the instance without creating a new instance.

Allow other activities to exist in this task

"Singleinstance" monopolizes a task, and other activities cannot exist in that task;

If it starts a new activity, regardless of the launch mode of the new activity, the new activity will run in other tasks (like adding the flag_activity_new_task parameter).

The other three modes can coexist with other activities.

Generate a new instance every time

"Standard" will generate a new instance of activity for each start of intent;

If the activity of "singletop" is at the top of the task stack, it will not generate a new instance of the activity, but directly use the instance at the top of the stack. Otherwise, it will generate an instance of the activity.

For example:

Now the task stack element is a-b-c-d (D is at the top of the stack). At this time, a start intent is sent to D. if D is "standard", a new instance of D is generated, and the stack becomes a-b-c-d-d.

If D is singletop, a new instance of D will not be produced, and the stack state is still a-b-c-d

If you send an intent to B at this time, no matter whether the launchmode of B is "standard" or "singletop", a new instance of B will be generated, and the stack state will change to a-b-c-d-b.

"Singleinstance" is the only activity on its stack, and it will be reused every time.

If the "singletask" is at the top of the stack, accept the intent. Otherwise, the intent will be discarded, but the task will still return to the foreground. When an existing activity instance processes a new intent, it will call the onnewintent () method. If an activity instance is generated from an intent, the user can return to the previous state through the back key; If an activity already exists to process the intent, the user cannot return to the previous state by pressing the back key.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>