Android 0 Basics Section 77th: Activity task stacks and startup modes

Source: Internet
Author: User

Through the previous study, the basic use of activity has been mastered, and then together to learn some of the more advanced content.

Android uses task stacks (tasks) to manage instances of activity. When you launch an app, Android creates a task buyers for it. The activity that starts first is pressed on the bottom of the stack, and then the activity that starts is placed in the top, and the boot mode can control how the activity is loaded in the task stack. This section explains in detail the task stack and startup mode for the activity.

One, activity task stack

When developing Android apps, it often involves a lot of system memory consumption, such as video playback, a large number of pictures or programs to open multiple activity is not closed in a timely manner, can cause program errors. To avoid this problem, Google provides a complete set of mechanisms for developers to control the task stack in Android.

The task stack in the Android system, similar to a container, is used to manage all activity instances. In the storage of activity, to meet the "advanced after-out (first-in/last-out)" principle. Next, a legend shows how the activity is stored in the task search, as shown in.

As you can see, the activity that is added to the task stack first is placed under the container, and then added to the container, and the activity from the task stack is the bottom of the activity.

However, using the task stack has the following drawbacks:

    • Each time a page is opened, an activity is added to the task stack, and the task stack is destroyed and the program exits only if the activity in the task stack is cleared out of the stack. This results in a poor user experience and requires multiple clicks to return the program to exit.

    • Each time you open a page, adding an activity to the task stack can also result in data redundancy, too much data duplication, and a memory overflow problem (OOM).

In order to solve the problem of task stack, Android has designed the startup mode for activity, then the following content will introduce the activation mode of activity in Android.

Second, activity startup mode

In real-world development, you should specify the appropriate startup mode for each activity based on specific requirements. There are 4 modes of activity activation, namely standard, Singletop, Singletask and Singlelnstance. In Androidmanifest.xml, you can set the startup mode by using the Android:launchmode property of the <activity> tag. The following are detailed explanations for these 4 startup modes.

1. Standard mode

Standard is the activity's default startup mode, and all activity uses standard mode without specifying the activity startup mode. Therefore, the activity used earlier is standard boot mode.

In standard mode, whenever a new activity is started, it enters the task stack and is at the top of the stack, and for activity using standard mode, the system does not determine whether the activity exists in the stack, and a new instance is created each time it is started.

Next, a legend shows how the activity is stored in the stack in standard mode, as shown in.

It can be seen that in standard start mode Activity01 the most advanced stack, followed by Activity02, the last is Activity03, when the stack, Activity03 first out of the stack, followed by Activity02, and finally Activity01 , to meet the "advanced after-out" principle.

2. Singletop mode

The Singletop mode is similar to standard, but when the activity that is started is already at the top of the stack, it is used directly without creating a new instance. If the activated activity is not at the top of the stack, a new instance is created at the top of the stack.

Next, a legend shows how the activity is stored in the stack in Singletop mode, as shown in.

As can be seen, the current stack at the top of the element is Activity03, if the interface is started again or Activity03, then re-use the current top of the activity instance, if the interface started again is not at the top of the stack, will recreate an instance.

3. Singletask mode

If you want activity to have only one instance in the entire application, you can use the Singletask mode, and when the activity's startup mode is specified as Singletask, each time you start the activity, the system first checks to see if there is an instance of the event in the stack. If the discovery already exists, the instance is used directly, and all activity on top of the current activity is stacked, and if no discovery is found, a new instance is created.

Next, a legend shows how the Singletask mode activity is stored in the search, as shown in.

As you can see, when ACTIVITY02 is started again, there is no new instance created, but instead the Activity03 instance is removed and the Activity02 instance is reused, which is Singletask mode, so that an activity has only one instance in the current stack.

4. SingleInstance mode

In program development, if you need activity to have only one instance of the entire system, then you need to use the singlelnstance mode. Unlike the above three modes, activity designated as Singlelnstance mode initiates a new task stack to manage the activity.

When the activity is loaded in singlelnstance mode, no matter which task stack starts the activity, only one instance of the activity is created and a new task stack is used to load the activity instance. Starting activity in this mode is divided into the following two scenarios:

First: If the activity to start does not exist, the system creates a new task stack, creates an instance of the activity, and adds the activity to the top of the stack, as shown in.

Second: If the activity you want to start already exists, regardless of which application or task is in the money, the system will move the activity's task stack to the foreground, so that activity is displayed.

At this point, the activity of the 4 start mode has been explained, in the actual development needs to choose the appropriate starting mode according to the actual situation.

Third, activity stack other configuration

In practical development, in addition to configuring the Android:launchmode property above to set the startup mode, the following properties are often configured to assist in managing the activity task stack:

    • Android:taskaffinity

    • Android:allowtaskreparenting

    • Android:cleartaskonlaunch

    • Android:alwaysretaintaskstate

    • Android:finishontasklaunch

Next, learn these 5 properties in two ways.

1, Affinity

By default, all activity in an application has a affinity, which makes them belong to the same task. Of course, each activity can also be set up with a separate affinity by the Android:taskaffinity property in <activity>. Activity in different applications can share the same affinity, and different activity in the same application can be set to a different affinity. The Affinity property works in the following 2 scenarios:

    1. When the intent object that initiates the ACTIVITY contains the flag_activity_new_task tag, the system will look for the activity that needs to start to have a different task than the current activity. If the affinity property of the activity to be started is not the same as the Affinity property of all the current tasks, the system creates a new task with that affinity property and presses the activity to start into the new task stack Otherwise, the activity is pressed into the same stack as the Affinity property.

    2. If the Android:allowtaskreparenting property of an activity is true, it can be moved from one task (TASK1) to another task (TASK2) with the same affinity (Task2 to the foreground). If an. apk file contains multiple "Applications" from the user's point of view, you may need to assign different affinity values to those activity.

2. Empty stack

When a user leaves a task for a long time (the current task is moved to the background), all activity outside the bottom of the task is purged. This way, when the user returns to the task, only the task's initial activity is left. We can change this behavior by modifying the following properties:

    • Android:alwaysretaintaskstate: If this property of the bottom activity is set to true, the above situation will not occur. All activity in the task is saved for a long time.

    • Android:cleartaskonlaunch: If this property of the bottom activity is set to true, once the user leaves the task, the activity in the task stack is emptied to only the bottom of the stack activity. This is just the opposite of android:alwaysretaintaskstate. The task returns to its initial state (only the bottom of the stack acitivty), even if the user is only momentarily away.

    • Android:finishontasklaunch is similar to Android:cleartaskonlaunch, but it only operates on a separate activity, not an entire task. It can end any activity, including activity at the bottom of the stack. When it is set to true, the current activity only exists as part of the task during the current session, and when the user exits the activity and returns, it does not exist.

The content of this issue is deeper, not very understood, if can not fully understand first know on the line, wait for a certain experience after later to study.

Come here today, if you have any questions welcome message to discuss together, also welcome to join the Android 0 Basic introductory Technical discussion group, grow together!

If the series of sharing is helpful to you, move your fingers, like, leave a message, your interaction is the greatest encouragement to me!

This article copyright for the public Share talent show (Shareexpert)--Xin 鱻 all, if necessary reprint please contact the author authorized, hereby declare!

Summary of past period review:

Android 0 Basics Introduction 1th: Android's past life

Android 0 Basics Section 2nd: Android system Architecture and application components those things

Android 0 Basics Section 3rd: Bring you up to talk about Android development environment

Android 0 Basics 4th: Installing and configuring the JDK correctly Ko fu the first trick

Android 0 Basics 5th: Use ADT bundles to easily meet the goddess

Android 0 Basics 6th: Configuration Optimization SDK Manager, official dating goddess

Android 0 Basics 7th: Take care of Android simulator and start the Sweet journey

Android 0 Basics 8th: HelloWorld, the starting point for my first trip

Android 0 Basics 9th: Android app, no code can be developed

Android 0 Basics Section 10th: Development IDE Big upgrade, finally ushered in Android Studio

Android 0 Basics Introductory Section 11th: Simple steps to take you to fly, run Android Studio project

Android 0 Basics 12th: Get familiar with the Android studio interface and start selling

Android 0 Basics 13th: Android Studio Personalized configuration to create a tool for development

Android 0 Basics 14th: Using high-speed genymotion, stepping into the rocket era

Android 0 Basics Section 15th: Mastering the Android Studio project structure, sailing

Android 0 Basics Section 16th: Android User Interface Development overview

Android 0 Basics Section 17th: text box TextView

Android 0 Basics Section 18th: Input box EditText

Android 0 Basics Get started section 19th: Buttons button

Android 0 Basics 20th: check box checkbox and radio button radiobutton

Android 0 Basics Section 21st: Switch Components ToggleButton and switch

Android 0 Basics Section 22nd: Image View ImageView

Android 0 Basics Section 23rd: Image button ImageButton and zoom button Zoombutton

Android 0 Basics Section 24th: Customize view simple to use to create your own controls

Android 0 Basics Section 25th: Simple and most commonly used linearlayout linear layouts

Android 0 Basics Section 26th: Two alignments, layout_gravity and gravity differ greatly

Android 0 Basics section 27th: Using padding and margin correctly

Android 0 Basics Section 28th: Easy to master relativelayout relative layout

Android 0 Basics 29th: Use tablelayout table layouts

Android 0 Basics 30th: Two minutes master Framelayout frame layout

Android 0 Basics Section 31st: absolutelayout absolute Layout with less

Android 0 Basics Section 32nd: New GridLayout Grid layout

Android 0 Basics section 33rd: Android Event Handling overview

Android 0 Basics Section 34th: Listening-based event handling in Android

Android 0 Basics Section 35th: Callback-based event handling in Android

Android 0 Basics Section 36th: Handling of Android system events

Android 0 Basics 37th: First Knowledge ListView

Android 0 Basics 38th: First Knowledge Adapter

Android 0 Basics section 39th: listactivity and custom list items

Android 0 Basics Section 40th: Customizing Arrayadapter

Android 0 Basics Section 41st: Using Simpleadapter

Android 0 Basics Section 42nd: Customizing Baseadapter

Android 0 Basics section 43rd: ListView Optimization and List End-to-end use

Android 0 Basics Section 44th: ListView Data Dynamic Update

Android 0 Basic Getting Started section 45th: Grid view GridView

Android 0 Basics section 46th: List Options Box spinner

Android 0 Basic Getting Started section 47th: AutoComplete text Box Autocompletetextview

Android 0 Basics Section 48th: Collapsible list Expandablelistview

Android 0 Basics section 49th: Adapterviewflipper picture Carousel

Android 0 Basics Section 50th: StackView card Stacking

Android 0 Basics Section 51st: progress bar ProgressBar

Android 0 Basics section 52nd: Customizing ProgressBar Cool progress bar

Android 0 Basics 53rd: Drag bar Seekbar and star rating bar Ratingbar

Android 0 Basics section 54th: View switch Components Viewswitcher

Android 0 Basics Section 55th: Imageswitcher and Textswitcher

Android 0 Basics Section 56th: Flip View Viewflipper

Android 0 Basics Section 57th: DatePicker and Timepicker selectors

Android 0 Basics Section 58th: Value selector numberpicker

Android 0 Basics Section 59th: Common Three clock clocks components

Android 0 Basics Section 60th: Calendar view CalendarView and timer chronometer

Android 0 Basics Section 61st: Scrolling view ScrollView

Android 0 Basics section 62nd: Search box Component Searchview

Android 0 Basics Introductory Section 63rd: Learn from the tab tabhost

Android 0 Basics 64th: Uncover Recyclerview

Android 0 Basics Section 65th: Recyclerview Split Line Development tips

Android 0 Basics Section 66th: Recyclerview Click event Handling

Android 0 Basics Section 67th: Recyclerview Data Dynamic Update

Android 0 Basics 68th: Recyclerview adding end-to-end views

Android 0 Basics Section 69th: Viewpager Quick Implementation Guide page

Android 0 Basics 70th: Viewpager build Tabhost effect

Android 0 Basics 71st: CardView Simple card-based layout

Android 0 Basics Section 72nd: Swiperefreshlayout drop-down refresh

Android 0 Basics Section 73rd: Activity creation and configuration

Android 0 Basics Section 74th: Activity startup and shutdown

Android 0 Basics Section 75th: Activity status and life cycle

Android 0 Basics Section 76th: Activity data saving and screen switching

Android 0 Basics Section 77th: Activity task stacks and startup modes

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.