4.1.3 Activity Mastery
Category Android Basic Tutorial
1. The Relationship Between Activity, Window, and View
>
Well, I originally wanted to understand the relationship between them, then I started looking into their calling process... After spending two hours, I only understood a very small part. Indeed, those who delve into the source code at the bottom level are great gods, like Lao Luo. I haven't reached that level yet. Below is my summary after consulting materials and looking at some source code. If there are any mistakes, please point them out! Here is a summary diagram:
Process Analysis: After an Activity calls startActivity, it will eventually call the attach method. Then, a PolicyManager implements an Ipolicy interface, followed by a Policy object. It then calls the makenewwindow(Context) method, which returns a PhoneWindow object. PhoneWindow is a subclass of Window. In this PhoneWindow, there is an inner class called DecorView, which is the root View of all application windows, that is, the boss of Views, directly controlling whether the Activity is displayed (quoting an old driver's words...). Well, inside it, there is a LinearLayout, which contains two FrameLayouts for holding the ActionBar and CustomView, respectively. The layout we load with setContentView() is placed in this CustomView!
Summarize the relationship between these three: To make a far-fetched analogy: We can regard these three classes as: a painter, a canvas, and the things painted by the brush; The painter draws patterns with the brush (LayoutInflater.inflate) and then paints them on the canvas (addView). Finally, it is displayed (setContentView).
2. Some Concepts of Activity, Task, and Back Stack
Next, let's understand the management mechanism of Android's Activity, which involves two terms: Task and Back Stack!
Concept Analysis:
Our APP is usually composed of multiple Activities, and Android provides us with a Task (task) concept, which is to collect multiple related Activities and then perform Activity transitions and returns! Of course, this Task is just a framework layer concept, and the data structure that implements Task in Android is the Back Stack (back stack)! I believe everyone is not unfamiliar with the stack data structure, and Java also has a Stack collection class! The stack has the following characteristics:
>
Last In First Out (LIFO), common operations are push (push), pop (pop), the one at the top is called the top of the stack, and the bottom is called the bottom of the stack
The Stack Stack in Android also has the above characteristics, and it manages Activities in the following way:
>
When switching to a new Activity, that Activity is pushed into the stack and becomes the top of the stack! And when the user clicks the Back button, the top Activity pops out, and the Activity that follows comes to the top of the stack!
Let's look at a flowchart given in the official documentation:
Process Analysis:
There are three activities A1, A2, and A3 in the application. When the user clicks the application icon on the Launcher or Home Screen, the main A1 is launched, then A1 opens A2, and A2 opens A3. At this time, there are three Activities in the stack, and these three Activities are in the same task (Task) by default. When the user presses the back, A3 pops out, and only A1 and A2 are left in the stack. Press the back button again, A2 pops out, leaving only A1 in the stack. Continue to press the back button, A1 pops out, the task is removed, and the program exits!
Then, I saw two other diagrams in the official documentation, out of curiosity, I looked at the explanation, and then discussed it with people in the group:
Then there is this explanation:
Then I summarized the conclusion:
>
Task is a collection of Activities, it is a concept, and the actual use of the Back Stack to store Activities, there can be multiple Tasks, but there is only one stack in front at the same time, and the others are in the background! So how is the stack generated?
Answer: When we open a new App by clicking the icon on the main screen, a new Task is created at this time! For example:
3. Task Management
1) Document Translation:
Okay, continue with the document, starting from the ManagingTasks in the document, the translation is as follows:
1) Document Translation
Continue with the document, starting from the ManagingTasks in the document, the translation is as follows:
>
As mentioned above, Android will add newly successfully launched Activities to the same Task and manage multiple Tasks At this point, there is still an instance of A in the task stack. If an instance of an activity launched in singleTop mode already exists in the task stack but is not at the top, then its behavior is the same as the standard mode, and multiple instances will also be created.
singleTask mode:
Only allows one instance of an Activity in the system. If there is already an instance in the system, the task holding this instance will move to the top, and the intent will be sent through onNewIntent().
If there is no instance, a new Activity will be created and placed in an appropriate task.
An issue mentioned in the official documentation:
>
The system will create a new task and instantiate this Activity as the root of the new task. This requires us to set the taskAffinity, and the resolution after using taskAffinity is as follows:
singleInstance mode
Ensures that no matter which Task starts the Activity, only one instance of the Activity will be created and added to the top of the new Task stack. That is to say, other activities started by this instance will automatically run in another Task. When this activity is launched again, the existing task and instance will be reused, and the onNewIntent() method of this instance will be called, passing the Intent instance to this instance. Like singleTask, there will only be one such Activity instance in the system at the same time.
5. Activity Leftovers
There may be some things about Activity that have not been mentioned yet, so a place is reserved here to make up for what is missed! First, it is suggested by the group friend Zhuhai-Kun to post the Activity management class of Open Source China, yes, this is posted, everyone can directly use it in the project~
1) Open Source China Client Activity Management Class:
package net.oschina.app;
import java.util.Stack;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
public class AppManager {
private static Stack<Activity> activityStack;
private static AppManager instance;
private AppManager(){}
/**
* Singleton instance
*/
public static AppManager getAppManager(){
if(instance==null){
instance=new AppManager();
}
return instance;
}
/**
* Add Activity to stack
*/
public void addActivity(Activity activity){
if(activityStack==null){
activityStack=new Stack<Activity>();
}
activityStack.add(activity);
}
/**
* Get the current Activity (the last one pushed into the stack)
*/
public Activity currentActivity(){
Activity activity=activityStack.lastElement();
return activity;
}
/**
* Finish the current Activity (the last one pushed into the stack)
*/
public void finishActivity(){
Activity activity=activityStack.lastElement();
finishActivity(activity);
}
/**
* Finish the specified Activity
*/
public void finishActivity(Activity activity){
if(activity!=null){
activityStack.remove(activity);
activity.finish();
activity=null;
}
}
/**
* Finish the Activity with the specified class name
*/
public void finishActivity(Class<?> cls){
for (Activity activity : activityStack) {
if(activity.getClass().equals(cls) ){
finishActivity(activity);
}
}
}
/**
* Finish all Activities
*/
public void finishAllActivity(){
for (int i = 0, size = activityStack.size(); i < size; i++){
if (null != activityStack.get(i)){
activityStack.get(i).finish();
}
}
activityStack.clear();
}
/**
* Exit the 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) { }
}
}
Summary of This Section:
Well, this section is here, and the things are quite bitter and difficult to understand. You can know it for now, and summarize the related operations of Task for overall scheduling:
>
Press the Home key to switch the previous Task to the background.
Long press the Home key, and the list of recently executed Tasks will be displayed.
Click the app icon in the Launcher or HomeScreen to start a new Task, or schedule an existing Task to the foreground.
When launching an Activity in singleTask mode, the system will search for whether there is an appropriate Task in the system. If there is, it will schedule this Task to the foreground to reuse this Task. If there is an instance of the Activity to be launched in this Task, all Activities above this instance
1.8 Project-Related Analysis (Various Files, Resource Access)
2.5.4 AutoCompleteTextView (Auto-Complete Text Box) Basic Usage
2.5.8 Notification (Status Bar Notification) Detailed Explanation
[3
8.3.4 Paint API — Xfermode and PorterDuff Detailed Explanation (I)
8.3.5 Paint API — Xfermode and PorterDuff Detailed Explanation (II)
8.3.6 Paint API — Xfermode and PorterDuff Detailed Explanation (III)
8.3.7 Paint API — Xfermode and PorterDuff Detailed Explanation (IV)
8.3.8 Paint API — Xfermode and PorterDuff Detailed Explanation (V)
8.3.14 Paint Several Enum/Constant Values and ShadowLayer Shadow Effects
8.3.17 Canvas API Detailed Explanation (Part 2) Clipping Method Collection
[8.3.