Analysis of four activity loading modes in Android programming

This paper analyzes and describes four activity loading modes of Android programming. Share with you for your reference, as follows:

Activity status

It is generally believed that an activity has the following four states:

① Active: when an activity is at the top of the stack, it is visible, focused and can accept user input. Android tries to keep it active as much as possible and kill other activities to ensure that the current active activity has enough resources to use. When another activity is activated, it 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 suspended, an activity will still be regarded as active, but user input is not acceptable. In very special cases, Android will kill a suspended activity to provide sufficient resources for the active activity. When an activity becomes completely hidden, it will become stopped.

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

④ Standby: after an activity is killed and installed, it is in standby state. The pending activity is removed from the activity stack and needs to be restarted before it is displayed and available.

Four loading modes of activity

In the multi activity development of Android, there may be many ways to jump between activities. Sometimes it is common to generate a new instance, and sometimes it is desirable to jump to an original activity instance instead of generating a large number of repeated activities. Loading mode is to decide how to start a jump to the original activity instance.

In Android, there are four activation modes, namely:

Standard: in standard mode, a new instance will be generated as soon as the startactivity () method is called.

Singletop: if an instance is already at the top of the activity stack, it will not generate a new instance, but just call the newinstance () method in the activity. If it is not at the top of the stack, a new instance will be generated.

Singletask: this instance will be generated in a new task. This instance will be used for each call in the future. No new instance will be generated.

Singleinstance: This is basically the same as singletask, with only one difference: in the task where the activity instance in this mode is located, there can only be this activity instance and no other instances.

Singletask: both the singletask pattern and the subsequent singleinstance pattern create only one instance. In this mode, no matter whether the jump object is an activity at the top of the stack or not, the program will not generate a new instance (of course, if there is already this instance in the stack). I think this model is quite useful.. In future multi activity development, multiple instances are often generated on the same page due to the jump relationship, which is always a little bad in the user experience. If you declare the corresponding activity as single task mode, this problem will no longer exist. However, I seem to have seen it again a while ago. Some people said that generally do not set other pages except the start page to the single task mode.. The reason is unknown for the time being. Anyone who knows can ask for advice.

Singleinstance: the explanations on the Internet seem to be more complicated. At the beginning, I didn't understand this model very much. I only know that it doesn't use much. Later, I read the online explanation carefully and understood it a little. Just explain it as I understand it. The activity set to singleinstance mode will monopolize a task (it feels that a task can be understood as a process). The activity monopolizing a task is not so much an activity as an application. This application is independent of other activities and has its own context activity. Take an example:

Now there are three activities: act1, ACT2 and Act3, where acti2 is the singleinstance mode. The jump relationship between them is: act1 -- ACT2 -- Act3. Now press the return key in Act3. Since ACT2 is located in an independent task and does not belong to the context activity of Act3, it will directly return to act1 at this time. This is the singleinstance mode. I don't know if the explanation is clear.

These startup modes can be set in the function manifest file androidmanifest.xml, and the launchmode attribute in.

What is a task

When we need an activity, we can start another activity. Maybe another activity is an activity defined in different applications.

In short, a task is an "application" in the user experience. It combines related activities and manages them in a stack (that is, the activity stack mentioned earlier), which is task.

On the Android platform, task can be simply understood as multiple activities working together to complete an application, regardless of which application the activity belongs to,

The task is started through the shortcut of application launcher and home screen, or from the recently used task record of "recent tasks" (hold down the home key for a long time).

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>