Introduction to the startup method and flag of the Activity, and the flag of the activity Method

Source: Internet
Author: User

Introduction to the startup method and flag of the Activity, and the flag of the activity Method

Activity Status:

Active: When an Activity is on the top of the stack, it is visible, focused, and user input acceptable. Android tries to maintain its Activity status as much as possible, killing other activities to ensure that the current Activity has enough resources to use. When another Activity is activated, it will be paused.

Pause: In many cases, your Activity is visible but has no focus. In other words, it is paused. The possible cause is that a transparent or non-full screen Activity is activated.

When paused, an Activity is regarded as an Activity, but user input is not acceptable. In special cases, Android will kill a paused Activity to provide sufficient resources for the Activity. When an Activity changes to completely hidden, it changes to stopped.

Stop: When an Activity is not visible, it is "STOPPED. This Activity will still store all its status and member information in the memory. However, when memory is needed elsewhere, it is most likely to be released. When an Activity is stopped, a very important step is to save the data and the current UI status. Once an Activity exits or is disabled, it will become available.

Waiting for use: After an Activity is killed and put in front of it, it is in the waiting state. To be used, Acitivity is removed from the Activity stack and needs to be restarted before display and availability.

Four loading modes of Activity:

In multi-activity development of android, there may be many ways to jump between activities. Sometimes it is common to generate a new instance and sometimes you want to jump to an original activity instance, instead of generating a large number of repeated activities. The loading mode determines the method to start a jump to an original Activity instance.

In android, there are four activity startup modes:

Standard: standard mode. A new instance is generated when startActivity () is called.

SingleTop: intent is used to create a new instance every time. Only one exception is allowed: when the activity at the top of the stack is the instance of the activity (that is, the instance to be created), no new instance is created. This solves the problem of stack top reuse.

SingleTask: When intent comes, check whether there is an instance of the activity in the stack. If so, send the intent to it; otherwise, a new instance of the activity will be created, place it at the bottom of a new task stack. It must be located at the bottom of a task stack, and only one of the activity instances can exist in the stack, but other activities are allowed to join the stack. It solves the problem of sharing an activity in a task.

SingleInstance: this is basically the same as singleTask. There is only one difference: In this mode, only the Activity instance can exist in the task where the activity instance is located, but not other instances. Once the instance of the activity in this mode already exists in a stack, any application will reuse the instance in the stack when activating the activity, so that multiple tasks can share the activity.

You can set these startup modes in the AndroidManifest. xml file of the function list to set the launchMode attribute.

Some features that affect the loading mode:

The core Intent flags include:

FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
FLAG_ACTIVITY_SINGLE_TOP
Core features include:
TaskAffinity
LaunchMode
AllowTaskReparenting
ClearTaskOnLaunch
AlwaysRetainTaskState
FinishOnTaskLaunch

The loading sequence of the Activity in the Activity stack (Task) can be controlled, which requires Intent Flag

Common Intent identifiers:

FLAG_ACTIVITY_BROUGHT_TO_FRONT
This flag is not set by the program code. For example, the system helps you set the singleTask mode in launchMode.
FLAG_ACTIVITY_CLEAR_TOP
If it is set and the Activity is already running in the current Task, it is no longer to restart an instance of the Activity, but all the activities above the Activity will be closed, the Intent is then delivered to the old Activity (currently at the top) as a new Intent.
For example, assume that A Task contains the Activity A, B, C, and D. If startActivity () is used in the call, and contains an Intent pointing to Activity B, both C and D end, and B receives the Intent. Therefore, the current stack status is A and B.
In the preceding example, the running Activity B can receive the new Intent in onNewIntent (), disable it, and restart it to receive the Intent. If its startup mode is declared as "multiple" (default), and you have not set the FLAG_ACTIVITY_SINGLE_TOP flag in this Intent, it will be disabled and re-created; for other startup modes, or set the FLAG_ACTIVITY_SINGLE_TOP flag in the Intent to ship the Intent to the onNewIntent () of the current instance.
This startup mode can also be used in combination with FLAG_ACTIVITY_NEW_TASK: used to start the root Activity in a Task. It will bring any running instance in that Task to the foreground, and then clear it until the root Activity. This is very useful, for example, when an Activity is started from Notification Manager.
FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
If this parameter is set, a restore point is set in the Activity stack of the Task. When the Task is restored, the Activity needs to be cleared. That is to say, when the next Task is marked to enter the foreground with FLAG_ACTIVITY_RESET_TASK_IF_NEEDED (the typical operation is that the user restarts the Activity on the main screen), the Activity and the above will be closed, so that users can no longer return to them, but can return to the previous Activity.
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
If set, the new Activity will not be saved in the list of recently started activities.
FLAG_ACTIVITY_FORWARD_RESULT
If you set and use the Intent to start a new Activity from an existing Activity, the Activity that replies to the target will be uploaded to the new Activity. In this way, the new Activity can call setResult (int), and the result value will be sent to the Activity that is used as the reply target.
FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
This flag is generally not set by the application code. if the Activity is started from the history (usually press the HOME Key), the system will help you set it.
FLAG_ACTIVITY_MULTIPLE_TASK
Do not use this flag unless you have implemented the application launcher yourself. It can be used with FLAG_ACTIVITY_NEW_TASK to disable sending existing tasks to the foreground. When set, the new Task will always start to process Intent, regardless of whether a Task can process the same thing.
Because the default system does not contain the graphical Task management function, you should not use this flag unless you provide users with a way to return started tasks.
If the FLAG_ACTIVITY_NEW_TASK flag is not set, this flag is ignored.
FLAG_ACTIVITY_NEW_TASK
If this parameter is set, the Activity becomes the start of a new Task in the history stack. A Task (from the Activity that starts it to the Activity in the next Task) defines the Activity atomic group that users can migrate. Tasks can be moved to the foreground and background. All activities in a specific Task are always in the same order.
This flag is generally used to present the "Start" type of behavior: they provide a series of things that can be done independently, and have nothing to do with starting their Activity.
With this flag, if the Task of the Activity being started is already running, the new Activity will not be started; instead, the current Task will be simply moved to the foreground. Refer to the FLAG_ACTIVITY_MULTIPLE_TASK flag to disable this line.
This flag cannot be used by the caller for the results of the Activity request that has been started.
FLAG_ACTIVITY_NO_ANIMATION
If it is set in Intent and passed to Context. startActivity (), this flag will prevent the system from applying the Acitivity migration animation when entering the next Activity. This does not mean that the animation will never run-if this flag is not specified before another Activity is started, the animation will be applied. This flag can be used to execute a series of operations, and animation is seen as the driver of a higher level of events.
FLAG_ACTIVITY_NO_HISTORY
If this parameter is set, the new Activity will not be retained in the history stack. The Activity is closed as soon as the user leaves it. You can also set the noHistory feature.
FLAG_ACTIVITY_NO_USER_ACTION
If this parameter is set, when a newly started Activity enters the foreground, this flag will block onUserLeaveHint () from the previous Activity callback before the Activity is paused ().
Typically, an Activity can depend on this callback to indicate that the Activity caused by an explicit user action is removed from the background. This callback marks an appropriate vertex in the lifecycle of the Activity and closes some notifications.
If an Activity is started through a non-user-driven event, such as a power-on or alarm clock, this flag should also be passed to Context. startActivity to ensure that the paused Activity does not think that the user knows its Notification.
Flag_activity_previus_is_top
If set and this intent is being used to launch a new activity from an existing one, the current activity will not be counted as the top activity for deciding whether the new intent shoshould be delivered to the top instead of starting a new one. the previous activity will be used as the top, with the assumption being that the current activity will finish itself immediately.
FLAG_ACTIVITY_REORDER_TO_FRONT
If it is set in Intent and passed to Context. startActivity (), this flag will cause the running Activity to move to the top of the historical stack.
For example, assume that A Task is composed of four activities: A, B, C, and D. If startActivity () is used to start Activity B by calling "D", B will move to the top of the history stack, and the current order will be A, C, D, and B. If the FLAG_ACTIVITY_CLEAR_TOP flag is also set, this flag will be ignored.
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
If set, and this activity is either being started in a new task or bringing to the top an existing task, then it will be launched as the front door of the task. this will result in the application of any affinities needed to have that task in the proper state (either moving activities to or from it), or simply resetting that task to its initial state if needed.
FLAG_ACTIVITY_SINGLE_TOP
If it is set, when this Activity is running at the top of the history stack, a new one is no longer started.

Note: When you start a new Activity from BroadcastReceiver or jump from Service to an Activity, do not forget to add FLAG_ACTIVITY_NEW_TASK as the Flag of Intent.

Related Article

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.