Android programming analysis of four Activity loading patterns

  • 2020-12-19 21:12:25
  • OfStack

This article analyzes four Activity loading modes of Android programming. To share for your reference, the details are as follows:

Activity state

Generally speaking, Activity has the following four states:

Active: when an Activity is at the top of the stack, it is visual, focused, and can accept user input. Android tries to keep it active as much as possible, killing the other Activity to ensure that the current active Activity has enough resources to use. When another Activity is activated, this will be paused.

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 was activated.

When paused, an Activity will still be active, but will not accept user input. In very special cases, Android will kill 1 suspended Activity to provide sufficient resources for active Activity. When an Activity becomes completely hidden, it becomes stopped.

Stop: When an Activity is not visible, it "stops". This Activity will still hold all of its state and membership information in memory. However, it is most likely to be freed up when memory is needed elsewhere. When an Activity is stopped, an important step is to save the data and the current UI state. Once 1 Activity exits or shuts down, it becomes available.

Pending: after an Activity has been killed and before it is installed, it is in the pending state. The Acitivity pending is removed from the Activity stack and needs to be restarted before it is displayed and available.

The four loading modes of activity

In the multi-activity development of android, a jump between activity may need to be done in a number of ways, sometimes by simply generating a new instance, sometimes by wanting to jump to an original activity instance rather than generating a large number of duplicate activity instances. The load mode is to decide how to start a jump to the original Activity instance.

In android, there are four startup modes of activity, which are respectively:

standard: Standard mode, 1 calls the startActivity() method and a new instance is generated.

singleTop: If an instance is already at the top of the Activity stack, no new instance is created, but the method newInstance() in Activity is called. If it is not at the top of the stack, a new instance is created.

singleTask: This instance will be generated in a new task. It will be used every time it is called. No new instances will be generated.

singleInstance: This is basically the same as singleTask with one difference: in the task instance of Activity in this mode, there can be only this instance of activity and no other instances.

The singleTask: singleTask pattern and the following singleInstance pattern are created with only one instance. In this mode, the program does not generate a new instance of activity, whether the object is on the top of the stack or not (provided, of course, that the instance is already on the stack). I find this model quite useful. In future multi-activity development, multiple instances of the same page will often be generated due to the jump relationship, which is always a bit bad for the user experience, but if you declare the corresponding activity as singleTask mode, this problem will not exist. But I think I've seen some people say 1, don't set all pages except the start page to singleTask mode. The reason is temporarily unknown, which know can consult next.

singleInstance: The explanations on the Internet seem complicated. I didn't understand this pattern very much at first, except that I didn't use it much. Later, I read the explanation on the Internet carefully and got a little understanding. Just interpret it as I understand it. activity set to singleInstance mode will hog 1 task (task feels like a process), and activity with 1 task will hog 1 application rather than activity, which is independent of the other activity and has its own context activity. Take an example:

There are now three activity: Act1, Act2, and Act3, where Acti2 is the singleInstance schema. The jump relationship between them is: Act1 -- Act2 -- Act3. Now press the return key in Act3. Since Act2 is located in a separate task, it does not belong to activity of Act3 context, it will be returned to Act1 directly at this time. This is the singleInstance pattern, I don't know if I'm clear.

These boot modes can be set in the functionality manifest file AndroidManifest.xml, which has the launchMode attribute.

What is a Task

When we need one Activity we can start another Activity, and maybe another Activity is defined in different applications.

In simple terms, an Task is an "application" on the user experience.
It combines the related Activity in one, managed as stack (the aforementioned Activity Stack), which is Task.

On the Android platform, task can be simply understood as multiple Activity working together to complete an application, no matter which Activity belongs to.

Task is activated by the shortcut of Application launcher, Home screen or by the Task record recently used by "Recent Tasks" (hold Home key for a long time).

I hope this article has been helpful for Android programming.


Related articles: