Android Development notes-go deep into Activity and Android note activity

Source: Internet
Author: User

Android Development notes-go deep into Activity and Android note activity

In the previous article "android development Note-recognition Activity", we learned the execution sequence of the Activity lifecycle and some basic data storage operations, however, if we only know that this is not enough for our development needs, we will continue to explore Activity today to learn about the differences between the four startup modes of Activity Task Stack and Activity.

 

Why do we need to know the task stack of Activity? The most direct manifestation is to improve user interaction friendliness.

For example, when we browse a news client, we enter the news details page, where there are two news titles separated by each other, when we click this title to enter the new news details page, what will happen if we do not control it? It creates Activity instances on n news details pages, causing users to launch multiple news details activities when exiting. This is very bad for user experience, of course, it is also very bad for our own programs. Constantly creating new activities will consume a certain amount of memory. Over time, applications will become increasingly stuck or even crashed. This knowledge point is also applied in my previous blog post "android Development notes-creating my own blog garden APP (4.

  

Before talking about the Activity Task Stack, we should first know what Stack is?

Simply put, we can compare the stack to an Kaifeng box. We can stuff in it. Here we assume that the bottom area of the stuff is the same as the bottom area of the box, these things have a certain order from bottom up. When we want to take out the things in the box, we cannot get the things at the bottom of the box, we can only get the top layer from top to bottom.

Let's take a look at this figure. The box here is the stack. The box port can be seen as the portal and exit of the stack. things represent data. Stack features: there is a certain order of first-in-first-out (the more first-in-first-out, the later it comes out ).

1. Activity Task Stack  

Now, after learning about what Stack is, we can start to enter the topic of today. In the official documentation of Android, we can know that the task stack is also a stack, has all the features of stack.

The Activity Task Stack, as its name implies, is the stack for storing Activity tasks. Here, the task stack is a box and Activity is something.

Each time we open an Activity, it will press an Activity into the Activity Task Stack. When we destroy an Activity, it will pop up an Activity from the Activity Task Stack, because of the Android system's own design, we can only get the focus of the current Activity on the mobile phone screen, that is, the top element of the stack (the top Activity), and the rest of the Activity will stay in the background for system calls.

 

1.1 concept of Task Stack:

The task stack is designed to improve user experience:
(1) When the program is opened, a task stack is created to store the activity of the current program. All the activities of the current program (including those called by the current program) belong to one task stack.
(2) A job stack contains a set of activities, which can be selected in sequence to interact with users. Only the activity at the top of the job stack can interact with users.
(3) The task stack can be moved to the background, and the status of each activity is retained, and their tasks are listed orderly for users without losing their status information.
(4) When you exit the application, when you clear all the activities in the Task Stack from the stack, the task stack will be destroyed and the program will exit.

1.2. disadvantages of Task Stack:

(1) Each time the page is opened, an Activity will be added to the task stack. Only when all the activities in the Task Stack are removed from the stack, the task stack will be destroyed and the program will exit, this results in poor user experience. You need to click "return" multiple times to exit the program.
(2) Every time the page is opened, an Activity will be added to the task stack, which may cause too much redundant data and cause memory overflow (OOM ).

 

2. Four startup methods of Activity

In order to solve the problem generated by the Task Stack, Android has designed the Startup Mode for the Activity. The following content introduces the Startup Mode of the Activity in Android, which is also one of the most important content.

LaunchMode plays an important role in the jump process of multiple activities. It can determine whether to generate a new Activity instance and whether to reuse an existing Activity instance, whether to share a task stack with other instances. A Task Stack is an object with a stack structure. A task stack can manage multiple activities. Each time an application is started, a corresponding Task Stack is created.

Activity has the following four launchMode modes: 1, standard 2, singTop 3, singTask 4, and singleInstance. We can configure them in AndroidManifest. xml <activity>The android: launchMode attribute of is one of the above four types.
2.1. practice is the only criterion for testing truth
The following is an example with two activities. Each Activity has a jump button. The first Activity clicks the button to jump to the second Activity, and the second Activity clicks the button to jump.

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.