Android-activity Start-up mode

Source: Internet
Author: User

Objective

In an Android application, it is unavoidable to include multiple activity, and when multiple activity is turned on, when the fallback key is pressed back to the previous activity, the concept of a fallback stack (back stack) needs to be learned to maintain this function. Here's a highlight:

    1. What is a fallback stack
    2. Status of Fallback stack
    3. Several startup modes of activity

What is a fallback stack?

First, let's look at the relationship between activity, application and process. As we all know, Android has four components: Activity, Service, Receiver, ContentProvider. Typically, an application is developed that contains multiple Android components, so the application is a collection of components, and the process is a running application (the carrier of these components).

The fallback stack (back stack) is for activity only, it is used to maintain the interface experience, the experience of the previous task makes the user feel an application, regardless of whether the activity from the same application, so do not confuse the fallback stack and process. The home page of the device is where most tasks start, and when the user clicks on an application icon, the application's task comes to the foreground and pushes the app's main activity back to the top of the stack and gets the focus, the activity called the root activity, The activity in the fallback stack can pop up the stack and destroy it by clicking the fallback key, which will cause the previous activity to focus until the user returns to the home page, and when the activity in the fallback stack is ejected and destroyed, the task ceases to exist. However, the process of this program still exists (not destroyed at this time).

Status of the task

Task Concept: Task is the concept of a virtual System Management application interface, typically an application creates a new task at startup, followed by this task, including activity that uses other applications. Of course, you can also start a task again by using a method such as the android:launchmode=singleinstance tag.

As described above, there is a fallback stack for each task, and there can be more than one task in the system, but only one task at a time receives the foreground focus, in general, the system allows the user to switch among multiple tasks, and the activity in the task in the background, will be placed in the stopped state. In fact, the activity in the same task, as long as it does not exist on top of the stack and receives the activity of the foreground focus, is a stopped state. An example diagram for the front and back of a task in an official document:

Activation mode of activity

Depending on the activation mode of the activity, its status in Backstack is different. Activity can be set by the Androidmanifest.xml manifest file configuration, Android:launchmode property in the <activity/> node. It has four options:

    • Standard
    • Singletop
    • Singletask
    • SingleInstance

Standard

The standard startup mode is also the default startup mode if the Android:launchmode property is not set. Activity in standard mode is pressed into the backstack in the order in which it was started.

is the stack and fallback operation for activity in standard mode:

Singletop

Single-top mode, this activity startup mode, when you start an activity, if you find that the activity is already present at the top of Backstack's stack, you will not recreate the new activity, but instead reuse the activity that already exists at the top of the stack. Avoid the same activity being repeatedly opened.

In Singletop mode, the activity's compression stack and fallback operation:

There are many scenarios for singletop, which are generally applicable to activity that can be reused and have multiple open channels, to avoid re-opening once an activity has been opened and the focus has been gained. For example, the bookmarks page of the Android system browser is an activity in singletop mode. Android's browser is written based on the WebKit kernel, which supports JavaScript scripting languages, and can be used to set browser bookmarks through JavaScript scripts, so that if there are multiple pages with a bookmark-saving JavaScript script, will cause the bookmark page to be opened multiple times, so the bookmark page is set to Singletop mode, which avoids repeatedly opening the bookmark page when you save multiple bookmarks.

Singletask

When you open an activity, check if there is an instance of the activity in the backstack, and if it exists, clear all other activity on the activity in Backstack.

In Singletask mode, the activity's compression stack and fallback operation:

Singletask's application scenario is the main page of the general program, and when you fall back to the main page, clear all the activity on top of the backstack, thus avoiding the confusion of the program navigation logic. such as the main page of the browser of the Android system, is Singletask mode, mentioned above, Android under the browser is the WebKit kernel, it is written in C language, and each time you open a new Web page if you reopen an activity, is very resource-intensive (parsing HTML, script scripts), so it is set to Singletask mode, so that in the browser application, no matter how many pages are opened, the same activity is used. So if there is a resource-intensive activity in the future, consider using Singletask open mode.

SingleInstance

Activity marked as SingleInstance startup mode, when started, will open a new backstack, this backstack only one instance of activity exists, and this backstack will get the focus. This is a very extreme pattern that will cause the entire device's operating system to have only one instance of the activity, regardless of where it was started.

In singleinstance mode, the activity's compression stack and fallback operation:

SingleInstance is generally suitable for scenarios where there is only one instance in the system, such as the call page of an Android system, where multiple calls are using an activity.

Of course, in Android, in addition to configuring the Launchermode property in the androidmanifest.xml manifest file, you can also set the startup mode in your code. In the component, starting an activity, you need to use the StartActivity () method, which passes a intent, you can use Intent.setflags (int flags) to set the startup mode of the newly started activity. The way in which the activity's startup mode is set by code, the priority is higher than the configuration in the Androidmanifest.xml manifest file.

The Intent.setflag (int flags) method passes an shaping data that is set by the Android system in order to be constant:

    • Flag_activity_new_task: This identity causes the newly initiated ACTIVITY to create a TASK independently.
    • Flag_activity_clear_top: This identifies whether the newly initiated activity check exists in the task, clears the activity above it if it exists, makes it focus, does not re-instantiate an activity, and generally combines flag_ Activity_new_task used together.
    • Flag_activity_single_top: Equivalent to the Launchermode property set to Singletop.

Summarize

This blog is mainly about the theoretical content, there is no sample code, nothing more than a few of the characteristics of the startup mode need to pay attention to understand the activity of the startup mode, can be a good user UI experience, the application of the promotion is very good, after all, a poor user experience of the application, It is very difficult to get the promotion of users.

Reference

Http://www.cnblogs.com/plokmju/p/android_activitylaunchermode.html

Android-activity Start-up mode

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.