This series begins to introduce the four main components of the activity, mainly to sort out relevant information about this part on the network. It contains the following three articles:
Android basics 05: four major components activity 01: Basic knowledge
Android basics 05: four major components activity 02: Activity and task
Android basics 05: four major components activity 03: startup mode instance
Through the previous blog, you should have a more intuitive understanding of the activity.
This article will continue to introduce the relationship between activity and task.
This article is mainly from blog posts: four Android activity loading modes, Android activity launchmode verification, and Android IOS Activity 2.
Activity Loading Mode
1. Activity Stack
As mentioned above, developers cannot control the status of the activity. What logic does the activity status operate? This requires you to know the activity stack.
The status of each activity is determined by its position in the activity stack (a later-in-first-out LIFO that contains all queues running the activity.
When a new activity is started, the activity of the current activity moves to the top of the activity stack.
If you use the back button to return the result, or the foreground activity ends, the activity on the stack will be moved up and become active. As shown in:
The priority of an application is affected by the activity with the highest priority. When determining whether an application ends to release resources, Android memory management uses stacks to determine the priority of activity-based applications.
2. Activity Status
Generally, activity has the following four States:
Active: when an activity is on the top of the stack, it is visible, focused, and user input acceptable. Android tries to maintain its activity status as much as possible, killing other activities to ensure that the current activity has enough resources to use. When another activity is activated, it will be paused.
Pause: in many cases, your activity is visible but has no focus. In other words, it is paused. The possible cause is that a transparent or non-full screen activity is activated.
When paused, an activity is regarded as an activity, but user input is not acceptable. In special cases, Android will kill a paused activity to provide sufficient resources for the activity. When an activity changes to completely hidden, it changes to stopped.
Stop: when an activity is not visible, it is "STOPPED. This activity will still store all its status and member information in the memory. However, when memory is needed elsewhere, it is most likely to be released. When an activity is stopped, a very important step is to save the data and the current UI status. Once an activity exits or is disabled, it will become available.
Waiting for use: after an activity is killed and put in front, it is waiting for use. To be used, acitivity is removed from the activity stack and needs to be restarted before display and availability.
3. Four loading modes of activity
In 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 you want to jump to an original activity instance, instead of generating a large number of repeated activities. The loading mode determines the method to start a jump to an original activity instance.
In Android, there are four activity startup modes:
Standard: standard mode. A new instance is generated when startactivity () is called.
Singletop: if an instance already exists at the top of the activity stack, a new instance is not generated, but the newinstance () method in the activity is called. If it is not at the top of the stack, a new instance is generated.
Singletask: This instance will be generated in a new task. This instance will be used for each call in the future and no new instances will be generated.
Singleinstance: this is basically the same as singletask. There is only one difference: In this mode, only the activity instance can exist in the task where the activity instance is located, but not other instances.
These startup modes can be set in the androidmanifest. xml file of the function list, and the launchmode attribute in <activity>.
Some symbols can be used in related Code. For example, if you want to enable only one instance, you can use intent. the flag_activity_reorder_to_front flag indicates that if the activity has been started, no new activity will be generated. You just need to add the activity instance to the top of the stack.
Intent intent = new intent (reorderfour. This, reordertwo. Class );
Intent. addflags (intent. flag_activity_reorder_to_front );
Startactivity (intent );
The loading mode of an activity is subject to the interaction control between the flag set in the intent object of the activity started and the feature value of the <activity> element of the activity in the manifest file.
The following are some features that affect the loading mode:
The core intent flags include:
Flag_activity_new_task
Flag_activity_clear_top
Flag_activity_reset_task_if_needed
Flag_activity_single_top
Core <activity> features include:
Taskaffinity
Launchmode
Allowtaskreparenting
Cleartaskonlaunch
Alwaysretaintaskstate
Finishontasklaunch
4. Differences between the four loading modes
1) differences between tasks
Generally, the target task of the activity of "standard" and "singletop" is in the same task as the receiver of the received intent, which is equivalent to who calls it, it is in the same task.
Unless Intent includes the flag_activity_new_task parameter. If the flag_activity_new_task parameter is provided, it is started into another task.
"Singletask" and "singleinstance" always take the activity to be started as the root element of a task and they will not be started into another task.
2) whether multiple instances are allowed
"Standard" and "singletop" can be instantiated multiple times and can exist in different tasks. In such instantiation, a task can include multiple instances of an activity;
"Singletask" and "singleinstance" only generate one instance and are the root element of the task.
Singletop requires that if an instance of the activity to be created at the top of the stack is created when the intent is created, the intent will be sent to the instance without creating a new instance.
3) whether other activities are allowed to exist in this task
"Singleinstance" occupies only one task, and other activities cannot exist in that task;
If it starts a new activity, regardless of the launch mode of the new activity, the new activity will run in other tasks (like adding the flag_activity_new_task parameter ).
The other three modes can coexist with other activities.
4) Whether to generate a new instance every time
"Standard" generates a new activity instance for each intent startup;
If the activity of "singletop" is at the top of the stack of the task, no new instance of the activity will be generated, and the instance at the top of the stack will be directly used. Otherwise, the instance of the activity will be generated.
For example:
Now the task stack element is A-B-C-D (D at the top of the stack), at this time to d send a start intent, if D is "standard", then generate a new instance of D, stack to a-B-c-d.
If D is singletop, no new instance of D will be produced and the stack status remains A-B-C-D
If intent is sent to B at this time, a new instance of B will be generated no matter whether the launchmode of B is "standard" or "singletop, stack status changes to a-B-c-d-B.
"Singleinstance" is the only activity in the stack where it is located. It will be reused every time.
If "singletask" is on the top of the stack, intent is accepted. Otherwise, the intent will be discarded, but the task will still return to the foreground. When an existing activity instance processes a new intent, the onnewintent () method is called. If an intent generates an activity instance, the user can return to the previous status through the back key; if an existing activity is used to process this intent, you cannot press the back key to return the previous status.
5. Android activity launchmode Verification
In Android, each activity has four launchmodes. The default mode is standard. The following describes how to perform instance verification for these four modes to deepen understanding.
Design two activities, called a and B. Put two buttons on each activity, one starting a and the other starting B.
The interface is as follows: (The above line of text is used to identify whether it is in interface a or interface B)
Implement oncreate () in the Code ():
Button btnA = (Button)findViewById(R.id.btnA); btnA.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub startActivity(new Intent(A.this, A.class)); } }); Button btnB = (Button)findViewById(R.id.btnB); btnB.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub startActivity(new Intent(A.this, B.class)); } }); Log.e("Task:"+getTaskId(), "A" + mId + " created.");
Onnewintent ():
Log.e("Task:"+getTaskId(), "A" + mId + " onNewIntent.");
Ondestroy ():
Log.e("Task:"+getTaskId(), "A" + mId + " destroyed.");
In this way, you can intuitively view the activity creation, destruction, and request response from the logcat output.
1. The launchmode of both actiity is the default standard. Start the AP and see the output:
Task: 9 (380): A1 created.
A is created for instance A1 and placed in task 9.
In this case, the content in the task stack is:
A
Click Start A on the page to see the output:
Task: 9 (380): A2 created.
A is created again with a new instance A2, which is still in task 9.
In this case, the content in the task stack is:
AA
Click Start B,
Task: 9 (380): B1 created.
B is created with a new instance B1, which is still in task 9.
The task stack content is:
AAB
Start B again,
Task: 9 (380): B2 created.
B is created with a new instance B2, which is still in task 9.
The task stack content is:
AABB
Press the back key to return the desktop in sequence, and the output is:
Job: 9 (380): B2 destroyed.
Job: 9 (380): B1 destroyed.
Task: 9 (380): A2 destroyed.
Task: 9 (380): A1 destroyed.
The task stack change order is:
AABB
AAB
AA
A
Conclusion:
Each intent creates a new activity to respond, and the back is processed in the order of the stack.
2. Set B to singletop and start AP:
Task: 10 (409): A1 created.
Restart:
Task: 10 (409): A2 created.
Restart B:
Task: 10 (409): B1 created.
Restart B:
Job: 10 (409): B1 onnewintent.
Conclusion:
When B is not at the top of the stack, a new B instance will still be created.
When B is at the top of the stack, the intent request to start B does not trigger the creation of a new instance of B, but triggers the onnewintent () at the top of stack B ()
Press the back key to return the desktop in sequence, and the output is:
Job: 10 (409): B1 destroyed.
Task: 10 (409): A2 destroyed.
Task: 10 (409): A1 destroyed.
Conclusion:
Although four intent messages are sent, only three activities are actually created, so only three outputs are destroyed.
3. Set B to singletask and start AP:
Task: 11 (438): A1 created.
Start B:
Task: 11 (438): B1 created.
Start:
Task: 11 (438): A2 created.
Start B:
Job: 11 (438): B1 onnewintent.
Task: 11 (438): A2 destroyed.
Conclusion:
If B already exists, the next request to B will trigger the onnewintent of the existing B1 instance;
In addition, if there are other activities on the job stack where B is located, other activities will be destroyed.
4. Set B to singleinstance and start AP:
Task: 12 (466): A1 created.
Start B. At this time, the new job stack 13 is created, and the previous A1 is not in the same job.
Task: 13 (466): B1 created.
Start B again, and no new instance is created:
Task: 13 (466): B1 onnewintent.
Start a and create a new instance in task 12.
Task: 12 (466): A2 created.
Press the back key:
Task: 12 (466): A2 destroyed.
There is no difference between the output and the previous one, but note that the interface is not switched to the B interface at this time, because the active task is 12 at this time, so A1 is displayed.
Press back again:
Task: 12 (466): A1 destroyed.
At this time, because task 12 has ended, task 13 of B1 becomes an active task, and the interface of B1 is displayed.