Four major Android components 3-activity (4) --> activity Startup Mode and Related Parameters

Source: Internet
Author: User

In the previous sections, we discussed the lifecycle, status saving, horizontal/vertical screen switching, and full screen display of an activity;

The following content is introduced:

1. How to start the activity;

2. How to return a value to the previous activity when closing the activity;

3. How to pass a value to the activity to be started;

4. Activity and task parameters.

How to start Activity

Activity display startAndroid provides two functions:

Public void startactivity (intent) is to directly start the activity;
Public void startactivityforresult (intent, int requestcode) to start the activity. The key point is that the value can be passed back when the started activity is returned to the previous activity;

Specify the acitivity to be started
For example, the following Java code explicitly specifies that B is to be started:

Intent intent= new Intent(this, B.class) 

Implicit startup of activity

Instead of explicitly specifying which activity to start, you can setNtent filter allows the system to filter the appropriate acitivity to start.
When startactivity is used, intent is implicitly parsed to a single activity. If multiple activities have the ability to execute a given action on specific data, Android will choose the best one to start.

Three matches are required. One is action, the other is category, and the other is data. Locate the activity to be started based on the three matching results.


Action implicit intent

Action matching indicates that the android intent filter contains specific actions or unspecified actions.

An intent filter has one or more defined actions. If no one matches the action specified by the intent, the intent filter is regarded as an action matching check failure.

The <intent-filter> element can contain sub-elements <action>, for example:
<Intent-filter>
<Action Android: Name = "com. example. Project. show_current"/>
<Action Android: Name = "com. example. Project. show_recent"/>
<Action Android: Name = "com. example. Project. show_pending"/>
</Intent-filter>
A <intent-filter> element must contain at least one <action>. Otherwise, no intent request can match the <intent-filter> element. If the action requested by intent matches a <action> in <intent-filter>, the intent passes the <intent-filter> action test.

CATEGORY implicit intent

Type matching is more strict. Intent filter must contain all types defined in the parsed intent. An intent filter without a specific type can only match an intent without a specific type.

The <intent-filter> element can contain <Category> sub-elements, such:
<Intent-filter...>
<Category Android: Name = "android. Intent. Category. Default"/>
<Category Android: Name = "android. Intent. Category. browsable"/>
</Intent-filter>
The intent request will pass the test only when all the category in the intent request matches the <Category> of an intentfilter in the component, the redundant <Category> declaration in intentfilter does not cause a matching failure.

Data implicit intent

The part of the intent data URI is compared with the data tag in the intent filter. If intent filter defines scheme, host/authority, path or mimetype, these values are compared with the intent Uri. Any mismatch will cause the intent filter to be deleted from the list. Android intent filter with no data value specified will match all intent data.
Mimetype is the data type of the data that is being matched. When matching data types, you can use wildcards to match child types (for example, earthquakes /*). If intent filter specifies a data type, it must match intent; if no data is specified, all match.

Scheme is the URI protocol. For example, http:, mailto:, tel :.
Host-name or "Data authority" is the part between scheme and Path in Uri (for example, www.google.com ). When the host name is matched, the scheme of the intent filter must also be matched.

The data path is followed by "Data authority" (for example,/ig ). The path matches only when both scheme and host-name match.

The description of data in <intent-filter> is as follows:
<Intent-filter...>
<Data Android: TYPE = "Video/MPEG" Android: Scheme = "HTTP".../>
<Data Android: TYPE = "audio/MPEG" Android: Scheme = "HTTP".../>
</Intent-filter>
The <DATA> element specifies the data Uri and Data Type of the intent request to be accepted. The URI is divided into three parts for matching: Scheme, authority, and path. The URI data type and scheme of the inteat request set with setdata () must be consistent with the one specified in intentfilter. If authority or path is also specified in intentfilter, they also need to match to pass the test.

Activity and task parameters in mainfest. xml

Android: allowtaskreparenting
Used to mark whether the activity can be moved from the started task to the task with affinity (when the task enters the foreground)

"True" indicates that the task can be moved, and "false" indicates that it must stay in the task at startup.

If this feature is not set, the value of the allowtaskreparenting attribute set to the <Application> element is applied to the activity. The default value is "false ".

Generally, when an activity is started, it is associated with the task that starts it and consumes its entire lifecycle. When the current task is no longer displayed, you can use this feature to force the activity to move to a task with affinity. A typical usage is to move the activity of an application to the main task of another application.

For example, if an email contains a link to a Web page, clicking it will start an activity to display the page. This activity is defined by the browser application, but now it is part of the email task. If it re-hosts to the browser task, when the browser enters the foreground next time, it will be visible, and when the email task enters the foreground again, it will not be seen.

The affinity of actvity is defined by the taskaffinity feature. The affinity of a task is determined by reading the affinity of the root activity. Therefore, the root activity is always located in tasks with the same affinity. Because the activity in the startup mode of "singletask" and "singleinstance" can only be located at the bottom of the task, re-hosting can only be limited to "standard" and "singletop" modes.

Android: alwaysretaintaskstate
Indicates whether the status of the task where the activity is located is always maintained by the system.

"True" indicates always; "false" indicates that the system is allowed to restore the task to its initialization State under certain circumstances. The default value is "false ".

This feature is only valid for the root activity of the task. For other activities, ignore it.
In general, in a specific situation, for example, when you reselect the task from the main screen, the system will clean up the task (delete all activivity located above the root activity from the stack ). In typical cases, this is also done when the user does not access this task for a period of time, for example, 30 minutes.

However, when this feature is set to "true", the user can always return to the latest status of the task, no matter how they start it. This is very useful. For example, for browser applications, there are many statuses (such as multiple open tabs), and users do not want to lose these statuses.

Android: cleartaskonlaunch
Used to mark whether to clear all the activities from the task, except the root activity (whenever the main screen is restarted)

"True" indicates that the root activity is always cleared, and "false" indicates no. The default value is "false ".

This feature is only meaningful to the activity (root activity) that starts a new task; it is ignored for other activities in the task.
When the value is "true", every time the user restarts the task, it will enter its root activity, no matter what the task is doing, whether the user uses back or home to exit. When the value is "false", the activity of the task may be cleared in some cases (refer to the alwaysretaintaskstate feature), but not always.

Assume that someone starts activity P from the main screen and migrates it to activity Q from there. Next, press home and return activity p. Generally, you may see activity Q, because it is the final work content of the P task. However, if p sets this feature to "true", when the user presses home and causes this task to enter the foreground again, all the activities on it (Q here) will be cleared. Therefore, when this task is returned, the user can only see P.

If both this feature and allowtaskreparenting are set to "true", the re-host activity will be moved to the task that shares affinity; the remaining activity will be discarded, as described above.

Android: finishontasklaunch
Indicates whether or not the existing activity instance needs to be closed (ended) when the user starts the task again (select this task on the main screen)

"True" indicates that it should be disabled, and "false" indicates that it is not disabled. The default value is "false ".
If both this feature and allowtaskreparenting are set to "true", this feature wins. The affinity of the activity is ignored. This activity will not be re-hosted, but will be destroyed.

Android: launchmode
Used to indicate how the activity is started. There are four modes, which work with the activity flags (flag_activity _ * variable) in the intent object to determine how the activity starts to process the intent. They are:

"Standard"
"Singletop"
"Singletask"
"Singleinstance"

The default mode is "standard ".
In the previous article, "loading modes of four Android activities" has been described in detail.

Android: nohistory
Used to mark whether the activity is cleared and ended from the activity stack when the user leaves the activity and is no longer visible on the screen (call the finish () method) -- "true ", it indicates that it should be disabled, and "false" indicates that it is not required. The default value is "false ".

The "true" value means that the activity does not leave any trace of history. Because it is not retained in the task of the activity stack, you cannot return it.

For example, you can use this feature to enable the interface.

Android: taskaffinity
This is the sharing of tasks described in this article.

Activity is an affinity of a task. An activity with the same affinity theoretically belongs to the same task (from the user's perspective, it is the same "application "). The affinity of a task is determined by its root activity.

Affinity determines two things: the re-host task of the activity (refer to the allowtaskreparenting feature) and the task of the activity host started with the flag_activity_new_task flag.

By default, all activities in an application have the same affinity. You can set this feature to reorganize them, or even place the activity defined in different applications into the same task. To make it clear that the activity does not host a specific task, set a null string for this feature.

If this feature is not set, the activity inherits from the application settings (refer to the taskaffinity feature of the <Application> element ). The default affinity of the application is the pakeage name set in the <manifest> element.


Flag Set in intent object related to task

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.

This is useful when your program has a split point. For example, an e-mail application may have an operation to view an attachment. You need to start the image browsing activity to display the attachment. This activity should be part of the E-mail application task because it is an operation triggered by the user in this task. However, when the user leaves this task and selects the email app from the main screen, we may want to go back to the viewing session, but not view the Image Attachment, because this is confusing. By setting this flag when starting Image Browsing, browsing and other started activities will be cleared the next time the user returns to the mail program.

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.

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.