Android Basics Getting Started tutorial--4.1.3 activity presenting illegal weapons
tags (space delimited): Android Basics Getting Started Tutorial
Introduction to this section:
Well, after studying the two-section activity, I believe you know how to use the activity, OK, this section is a bit deeper, we go down
The bottom, but not the elegant source, not yet to that level. Let's take a look at the relationship between activity and Window,view, which is loading actvitiy
Go through the process, and then we walk down the document to learn the activity Management mechanism: task and back Stack, then look at Android to provide us
Allow to manage the related properties of the task, and finally focus on the activity of the four modes of startup mode, OK, start the content of this section!
The relationship between 1.activity,window and view
Well, I wanted to get to know a couple of their relationships, and then we started to look at their calling process ... Results buckle for two hours, only understand a very small and small part, sure enough, to the bottom of the source is big God, such as Old Luo, not to that grade, the following is their own access to information, read the next point of the source of the induction, if it is wrong to write the welcome point! Below is a summary chart:
Process Analysis:
After the activity calls StartActivity, the Attach method is finally called, then a Ipolicy interface is implemented in Policymanager, then a policy object is implemented, followed by a call to Makenewwindow (Context) Method, the method returns a Phonewindow object, and the Phonewindow
Is the subclass of window, in this phonewindow there is a decorview inner class, is the root view of all application windows, that is, the boss of view,
Direct control of activity display (quoting old driver's words ...), OK, then there is a linearlayout inside, and there are two more framelayout they are to install Actionbar and CustomView respectively, and our Setcontentview () loaded layout is placed in this CustomView!
summarize the relationship of these three:
To make a far-fetched analogy:
We can think of these three classes as: painters, canvases, brushes.
The painter draws the pattern through the brush ( layoutinflater.infalte) and then draws it on the canvas (addview)!
Show it at last (Setcontentview)
Some concepts of 2.activity,task and back stack
Then we understand the management mechanism of activity in Android, which involves two nouns: task and back Stack!
Concept Analysis:
Our apps are typically made up of multiple activity, and in Android we provide a task concept,
is to collect a number of related activity and then jump and return the activity! Of course, this task is just a
The concept of the Frameworker layer, and the data structure that implements the task in Android is the backstack.
I believe you are not unfamiliar with the data structure of the stack, Java also has a stack of the collection Class! The stack has the following characteristics:
FIFO (LIFO), commonly used operations into the stack (push), out of the stack (pop), at the top of the call stack top, the bottom is called the stack bottom
The stack stack in Android also has these features, and he manages the activity:
When you switch to a new activity, the activity is pushed into the stack and becomes the top of the stack!
And when the user clicks the back key, the activity of the top of the stack, immediately after the activity came to the top of the stack!
Let's look at a flowchart given in the official documentation:
Process Analysis:
There are a1,a2,a3 three activity in the application, and when the user clicks on the application icon in launcher or home screen,
Start the main A1, then A1 Open the A2,a2 open A3, when there are three activity in the stack, and the three activity by default in
The same task, when the user presses back, pops up A3, only A1 and A2 are left in the stack, and then the return key is pressed.
Pop-up A2, the stack only left A1, and then continue to press the Back button, pop A1, the task is removed, that is, the program quit!
Then I saw another two figures in the official document, curious, and I looked at the explanations and discussed the following with the people in the group:
And then there's the explanation:
And then summed up the conclusion:
A task is a collection of activity, a concept that actually uses the back stack to store activity and can have multiple tasks, but
At the same time there is only one stack at the front, others are in the background! How did that stack arise?
A: When we click on the icon to open a new app via the home screen, a new task! is created. As an example:
We open the app by clicking on the Mobile app icon, this time we will create a new stack 1, and then start adding the newly generated activity, maybe we opened the SMS App page in the Address Book app, but not create a new stack at this time, but continue to add to the stack 1, which is
Android advocates a way of user experience, that is, switching between different applications can make users feel like the same application,
A consistent user experience, officially known as the seamless (seamless connection)!
—————— this time if we click on the Home button, back to the home screen, when Stack 1 into the background, we may have the following two actions:
1) Click the menu key (the square button), click Open the program, and then stack 1 back to the front desk!
Or we can click on the main screen of the communication record icon, open the app, at this time will not create a new stack, stack 1 back to the front desk!
2) If we click on another icon to open a new app, then we will create a new stack 2, stack 2 will go to the foreground,
and Stack 1 continues to stay in the background;
3) The following is also the case ... And so on
3.Task management 1) document translation:
All right, go ahead. Document, starting with the Managingtasks in the document, the approximate translation is as follows:
1) Document translation
Proceed to the documentation, starting with the Managingtasks in the document, translated as follows:
As mentioned above, Android adds the newly successfully activated activity to the same task and manages multiple tasks in first in, out
And back stack, the user doesn't have to worry about how activites interacts with task tasks or how they exist in the go stack!
Perhaps you want to change this normal way of managing. For example, you want one of your activity to be able to manage in a new task;
Or you just want to instantiate an activity, or you want to clean up all activities in the task except the root activity when the user leaves the task. You can do these things or more, just by modifying the Androidmanifest.xml
< activity > Related attribute values or intent to startactivity () in code by passing special identifiers can be easily implemented
The management of the actvitiy.
The properties we can use in < activity > are as follows:
- Taskaffinity
- Launchmode
- Allowtaskreparenting
- Cleartaskonlaunch
- Alwaysretaintaskstate
- Finishontasklaunch
The main intent signs you can use are:
- Flag_activity_new_task
- Flag_activity_clear_top
- Flag_activity_single_top
OK, here's how it's used:
2) Taskaffinity and allowtaskreparenting
By default, all activity in an application has a affinity, which makes them belong to the same task.
You can understand whether a flag is in the same task, however, each activity can be
The Taskaffinity property in the < activity> sets a separate affinity.
Activity in different applications can share the same affinity, different activity in the same application
can also be set to a different affinity.
The Affinity property works in 2 situations:
1) When starting ACTIVITY, the intent object contains the flag_activity_new_task tag:
When the intent object passed to StartActivity () contains a flag_activity_new_task tag, the system will look for the activity that needs to start with a different task than the current activity. If the affinity property of the activity to be started is not the same as the Affinity property of all the current tasks, the system creates a new task with that affinity property and presses the activity to start into the new task stack Otherwise, the activity is pressed into the same stack as the Affinity property.
2)allowtaskreparenting Property set to True
If the Allowtaskreparenting property of an activity is true, it can be moved from one task (TASK1) to another task (TASK2) with the same affinity (Task2 to the foreground).
If an. apk file contains multiple "Applications" from the user's point of view, you may need to assign different affinity values to those activity.
3) Launchmode:
Four selectable values, starting mode the core of our research, the following details!
They were: Standard(default),singletop,singletask,singleinstance
4) Empty Stack
When a user leaves a task for a long time (the current task is moved to the background), the system clears all activity outside the activity in the task
。 This way, when the user returns to the task, only the task's initial activity is left. We can modify the following properties to
Change this behavior!
alwaysretaintaskstate:
If this property of the bottom activity is set to true, the above scenario will not occur. All activity in the task is saved for a long time.
Cleartaskonlaunch
If this property of the bottom activity is set to true, once the user leaves the task,
The activity in the task stack is emptied to only the bottom of the stack activity. This situation coincides with
Alwaysretaintaskstate opposite. The task returns to its initial state even if the user is only momentarily away
(Only the bottom acitivty is left).
Finishontasklaunch
Similar to Cleartaskonlaunch, but it is only for individual activity
Instead of the entire task. It can end any activity, including activity at the bottom of the stack.
When it is set to true, the current activity only exists as part of the task during the current session,
When the user exits the activity and returns, it does not exist.
The four loading modes of the 4.Activity are detailed:
Next, let's explain the next four loading modes in detail:
They were: Standard(default),singletop,singletask,singleinstance
In the online day to see an illustrated explanation of the start-up mode, very good, it may be easier to understand it, here for reference:
Original link: Activity startup mode: Standard, Singletop, Singletask and SingleInstance
English Original: Understand Android Activity ' s Launchmode:standard, Singletop, Singletask and SingleInstance
There is also a detailed description of the loading mode: Android activity four startup mode and Taskaffinity properties
Take a look at the summary chart first:
Model Details:
Standard mode:
The standard startup mode is also the default startup mode for activity. Activity initiated in this mode can be instantiated multiple times, where multiple instances of activity can exist in the same task, and each instance processes a intent object. If activity A's startup mode is standard, and a is started, and activity A is started again in a, call StartActivity (The new Intent (this, A.class), an instance of a is started again on the top of a, that is, the state in the current buyers is a–>a.
Singletop mode:
If an instance of activity started in Singletop mode already exists on the stack top of the task stack,
When you start this activity again, you don't create a new instance, but instead reuse that instance at the top of the stack,
The onnewintent () method of the instance is called to pass the intent object to this instance.
For example, if the startup mode of A is singletop, and an instance of a is already present on the top of the stack,
Then when you call StartActivity (new Intent (This,a.class)) to start A,
The instance of a is not created again, but the original instance is reused and the Onnewintent () method of the original instance is called.
In this case there is still an instance of a in the task stack. If an instance of activity is started in Singletop mode
already exists with the task stack, but not at the top of the stack, its behavior is the same as standard mode, and multiple instances are created.
Singletask mode:
Only one activity instance is allowed in the system. If there is already an instance in the system,
The task that holds this instance will move to the top, while intent will be sent via Onnewintent ().
If not, a new activity is created and placed in the appropriate task.
An issue that is mentioned in the official documentation:
The system creates a new task and instantiates the activity as the root of the new task (root)
This will require us to set up the taskaffinity, using taskaffinity after the dismissal:
SingleInstance mode
Ensure that the system will only create an activity instance and add it to the top of the new task stack, regardless of which task it initiates activity from
This means that other activity that is initiated by the instance will automatically run in another task.
When you start an instance of the activity again, the existing tasks and instances are reused. And this instance is called
Onnewintent () method to pass the intent instance to the instance. Same as Singletask,
Only one such activity instance will exist in the system at the same time.
5.Activity Supplements
For activity there may be some things not mentioned, here reserved a location, the missing will be here to fill up!
First is the group of Friends Zhuhai-Kun's proposal, the open source China's activity management class also affixed, well, this is affixed, we can directly use
In the Project ~
1) Open source China client activity Management class:
PackageNet.oschina.app;ImportJava.util.Stack;Importandroid.app.Activity;ImportAndroid.app.ActivityManager;ImportAndroid.content.Context; Public class AppManager { Private StaticStack<activity> Activitystack;Private StaticAppManager instance;Private AppManager(){}/** * Single instance * / Public StaticAppManagerGetappmanager(){if(instance==NULL) {instance=NewAppManager (); }returnInstance }/** * Add activity to stack * / Public void addactivity(Activity activity) {if(activitystack==NULL) {activitystack=NewStack<activity> (); } activitystack.add (activity); }/** * Gets the current activity (last pressed in the stack) */ PublicActivitycurrentactivity() {Activity activity=activitystack.lastelement ();returnActivity }/** * End the current activity (last pressed in the stack) */ Public void finishactivity() {Activity activity=activitystack.lastelement (); Finishactivity (activity); }/** * End the specified activity * / Public void finishactivity(Activity activity) {if(activity!=NULL) {Activitystack.remove (activity); Activity.finish (); activity=NULL; } }/** * End activity for specified class name * / Public void finishactivity(class<?> CLS) { for(Activity Activity:activitystack) {if(Activity.getclass (). Equals (CLS)) {finishactivity (activity); } } }/** * End All activity * / Public void finishallactivity(){ for(inti =0, size = Activitystack.size (); i < size; i++) {if(NULL! = Activitystack.get (i)) {activitystack.get (i). Finish (); }} activitystack.clear (); }/** * Exit application * / Public void AppExit(Context context) {Try{finishallactivity (); Activitymanager activitymgr= (Activitymanager) Context.getsystemservice (Context.activity_service); Activitymgr.restartpackage (Context.getpackagename ()); System.exit (0); }Catch(Exception e) { } }}
This section summarizes:
OK, this section is here, things are more bitter difficult to understand, temporarily know the next can, summarize the task of the overall scheduling
Related operations:
- Press the home key to switch the previous task to the background
- Long press the home key to display a list of recently executed tasks
- Click the app icon in launcher or homescreen to open a new task, or dispatch an existing task to the foreground
- When you start activity in the Singletask mode, the system is searched for a suitable task, and if it exists, it is dispatched to the foreground to reuse the task. If there is already an instance of activity in this task that is to be started, clear all activity on top of the instance and display the instance to the user. If there is no instance of the activity to start in this existing task, start an instance at the top of the task. If this task does not exist, a new task is started and an instance of the activity of this singletask mode is started in this new task.
- When the activity of SingleInstance is started, it searches the system for an instance of the activity that already exists, and if it does, dispatches the task to the foreground. Reusing the instance of this activity (which is the only activity in the task), if it does not exist, opens a new task and launches an instance of the activity of the singleinstance pattern in this new task.
Ok this section is here, about the task and activity loading mode of things is still more complicated, below for everyone to write this article
Time of some references, you can see for yourself ~
Reference documents:
1.Tasks and Back Stack
2. Understanding the relationship between activity and task in Android
3.Activity startup mode: Standard, Singletop, Singletask and SingleInstance
4.Understand Android Activity ' s Launchmode:standard, Singletop, Singletask and SingleInstance
5.Android activity four startup modes and Taskaffinity properties
6.Android of activity and tasks
7.Activity of four startup modes and Onnewintent ()
8. The Android task and the return stack are fully parsed, counting the details that you don't know
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started tutorial--4.1.3 activity presenting illegal weapons