Analysis of four activity loading modes of Android programming _android

Source: Internet
Author: User
Tags home screen

This article analyzes four kinds of activity loading modes of Android programming. Share to everyone for your reference, specific as follows:

Activity status

Activity is generally considered to have the following four states:

① Activities : When an activity is on top of the stack, it is visible, focused, and acceptable to user input. Android tries to maintain its active status as much as possible, killing other activities to ensure that the current activity has enough resources to use. When another activity is activated, this will be suspended.

② Pause : In many cases, your activity is visible but it has no focus, in other words it is paused. It is possible that a transparent or non full-screen activity is activated.

When paused, an activity is still active, but no user input can be accepted. In very special cases, Android will kill a suspended activity to provide sufficient resources for activities. When an activity becomes completely hidden, it will become a stop.

③ Stop : When an activity is not visible, it "stops". This activity will still hold all of its status and membership information in memory. However, when memory is needed elsewhere, it is most likely to be released. When an activity stops, an important step is to save the data and the current UI state. Once an activity exits or closes, it becomes a pending state.

: It is to be used when an activity is killed and before it is loaded. The pending acitivity is removed from the activity stack and needs to be restarted before it is displayed and available.

Four kinds of loading modes for activity

In Android's multiple activity development, the jump between the activity may need to be a variety of ways, sometimes ordinary to generate a new instance, and sometimes want to jump to the original activity instance, rather than generate a lot of repetitive activity. Load mode is deciding which way to start a jump to a previous activity instance.

In Android, there are 4 activation modes for activity, respectively:

standard: Standard mode, a call to the StartActivity () method produces a new instance.

singletop: If an instance already exists at the top of the activity stack, no new instances are generated, but only the newinstance () method in the activity is invoked. If not at the top of the stack, a new instance is generated.

Singletask: this instance will be generated in a new task, which will be used every time in the call, and will not produce a new instance.

singleinstance: This is basically the same as Singletask, there is only one difference: In this model of the activity instance in the task, there can be only this activity instance, no other instances.

singletask: singletask mode and subsequent singleinstance mode are all created only one instance. In this mode, the program does not generate a new instance, regardless of whether the jump object is an activity at the top of the stack (the premise is that the instance already exists in the stack). I think this pattern is quite useful. In later multiple activity development, often because of the relationship between jumps cause the same page to generate multiple instances, this is always a bit bad in the user experience, and if you are the corresponding activity declared as Singletask mode, this problem will no longer exist. But I think I've seen someone say that. Generally do not set other pages except the start page to Singletask mode ... The reason is temporarily unknown, who knows can consult.

SingleInstance: Looking at the explanations on the Internet seems to be more complicated. In the beginning also did not understand this mode, only know that it is not used much. Then read the online explanation, a little understand. Explain it to my understanding. An activity set to SingleInstance mode will monopolize a task (the sense task can be understood as a process), and the activity that is exclusive to a task is more an application than an activity. This application is independent of other activity, and it has its own context activity. Take an example to illustrate it:

There are now three Activity:act1, Act2, Act3, Acti2 in singleinstance mode. The jump relationship between them is: Act1-Act2-Act3, now press the return key in Act3, because Act2 is in a separate task, it does not belong to the ACT3 context activity, so it will be returned directly to Act1. This is the singleinstance mode, do not know the explanation is clear.

These boot modes can be set in the feature manifest file Androidmanifest.xml, in the Launchmode property.

What is a task

When we need an activity to start another activity, perhaps another activity is defined as an activity in a different application.

Simply put, a task is an "application" of the user experience.
It combines the related activity and manages it in a Stack (the activity stack mentioned earlier), which is the task.

On the Android platform, the task can be simply understood as a collaborative effort by multiple activity to accomplish an application, regardless of the specific application that the activity belongs to.

Tasks are started by Application Launcher, a shortcut to the home screen, or a task record that was recently used by the "recent Tasks" (holding down the home key for a long time).

I hope this article will help you with your Android programming.

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.