Android Initial Learning-four startup modes for activity (fallback stack) __java

Source: Internet
Author: User
Tags rollback

In an Android application, there will inevitably be multiple activity, and when you turn on multiple activity, when you press the Back button, you typically fall back to the previous activity, which is a bit like the browser. But note that the Android system can only fall back to the previous activity and cannot move on to the next one, and maintaining this functionality requires understanding the concept of a fallback stack (back stack).

This blog is focused on what is the fallback stack back to the state of the stack several startup modes of activity

What is a fallback stack.

First, let's look at the relationship between the application and the 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 processes are the carriers that run those components.

The fallback stack (back stack) is for the activity only, it is used to maintain the interface experience, so that a task let the user feel is an application, regardless of whether the activity is from the same application, so do not confuse the fallback stack and process. The home page of the device is the starting point for most tasks, and when a user clicks on an application icon, the application task comes to the foreground and pushes the application's main activity into the top of the Backstack stack, which is called the root activity , and the activity in Backstack can be ejected and destroyed by clicking the Rollback key, which will get the focus of the previous activity until the user returns to the home page, and when the activity in the Backstack is ejected, The task ceases to exist, but the process of the program still exists (not destroyed at this time).

Status of Task

As described above, each task has a backstack, and there can be multiple tasks in the system, but each time only one task gets the foreground focus, and in general, the system allows the user to switch among multiple tasks, and the activity in the background task, will be placed in the stopped state. In fact, the activity in the same task, as long as it does not exist at the top of the stack and gets the activity of the foreground focus, then it is a stopped state. The following figure is an example diagram of a task before and after the table in an official document:

Start mode of activity

Depending on the starting mode of the activity, its state in the Backstack is different. The activity can be set through the Androidmanifest.xml manifest file configuration, Android:launchmode property in the <activity/> node. It has four options: Standard Singletop singletask singleinstance

Standard

Standard boot mode, also the default boot mode, if the Android:launchmode property is not set. Activity in standard mode is pressed into the backstack in the order in which they are initiated.

The following figure is a schematic diagram of the activity's compaction stack and rollback operation in standard mode:

Singletop

Single-top mode, this activity initiation mode, starts an activity when it is discovered that the Backstack's stack has already existed, it will not re-create the new activity, but reuse the existing activity on the top of the stack, Avoid the same activity being repeatedly opened.

The following figure is a schematic diagram of the activity's compaction stack and rollback operation in Singletop mode:

There are many scenarios for singletop that can be reused and have multiple open channels, to avoid the repetition of an activity when it has been opened and focused. The Android browser's bookmark page, for example, is a singletop-mode activity. Android's browser is based on the WebKit kernel, which supports JavaScript scripting languages, and can set browser bookmarks through JavaScript scripts, so if there are multiple pages with JavaScript scripts for saving bookmarks, will cause the bookmark page to be opened more than once, so the bookmark page is set to Singletop mode, which avoids repeatedly opening the bookmark page when you save multiple bookmarks.

Singletask

When opening an activity, check to see if there are instances of this activity in the backstack, and if so, backstack all other activity in the activity.

The following figure is a schematic diagram of the activity's compaction stack and rollback operation in Singletask mode:

Singletask's application scenario is the main page of the general program, and when you fall back to the main page, clear the Backstack, all the activity on top of it, thus avoiding the confusion of the program navigation logic. For example, the homepage of the Android browser is the Singletask mode, which mentions that the Android browser is the WebKit kernel, which is written in C, and each time a new page is opened to reopen an activity, is very resource intensive (requires parsing HTML, script scripts), so it is set to Singletask mode, so that in the browser application, no matter how many pages are open, the same activity is used. So if you have an activity that consumes system resources later, consider using the Singletask open mode.

SingleInstance

The activity that is marked as the SingleInstance startup mode opens a new Backstack, in which only one instance of the activity exists, and the backstack gets the focus. This is an extreme pattern that causes the entire device's operating system to have only one example of this activity, regardless of where it was started.

The following figure is a schematic diagram of the activity's compaction stack and rollback operation in singleinstance mode:

SingleInstance generally applies to scenarios where only one instance of the system exists, such as the Android phone call page, where multiple calls are made using an activity.

Of course, in Android, in addition to configuring the Launchermode attribute in the androidmanifest.xml manifest file, you can set the startup mode in code. In an assembly, a startactivity () method is used to initiate an activity, where a intent is passed, and the intent.setflags (int flags) can be used to set the startup mode of the newly initiated activity. The way in which the activity's startup mode is set by code is higher than the configuration in the Androidmanifest.xml manifest file.

The Intent.setflag (int flags) method passes a reshaping of data that is set by the Android system for constants: Flag_activity_new_task: This identity will enable the newly initiated activity to create a TASK independently. Flag_activity_clear_top: This identification will enable the newly initiated activity check to exist in the task, and if it exists, clear the activity above it, so that it receives focus, does not instantiate an activity, and generally combines the Flag_ Activity_new_task used together. Flag_activity_single_top: Equivalent to Setting the Launchermode property to Singletop.

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.