What is the role of Window
Window is the core of Android window system and plays multiple roles, including:
Interface drawing: Window is responsible for drawing the user interface defined by Activity to the screen, including the layout and view drawing.
Event distribution: Window receives and distributes user input events, such as touch, keyboard, and gesture events, for handling by the appropriate View.
Window Management: The WindowManager service is responsible for managing the hierarchy, position, and size of all application windows, ensuring the correct order of window overlays and handling focus changes.
Multi-window support: Window supports multi-window mode, allowing multiple applications or activities to run simultaneously on the same screen, providing more flexibility for multitasking.
In short, there is a close connection between Activity and Window, which is a core component of the Android user interface and is responsible for the display, interaction and management of the user interface.
The relationship between Activity and Window
In Android applications, Activity is the main interaction point and organizer of the user interface. Each Activity is closely associated with a Window, which represents a drawable area responsible for displaying the user interface of the Activity.
Activity is responsible for defining and managing the content of the user interface through methods such assetContentView
To specify what to display in Window. For example:
@Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); }
WindowManager
WindowManager is a key system service in the Android system. It manages the display, layout, location, size and hierarchy of application windows.
The role of WindowManager
WindowManager plays the following key roles in the Android system:
Window Management: WindowManager manages the display and layout of all application windows, ensuring that the windows are stacked in the correct order so that users can interact with them.
Position and size control: WindowManager allows you to control the position and size of the window, which is useful for creating custom windows, floating windows, or pop-up dialog boxes.
Window type and hierarchy: By using window types and hierarchies, WindowManager controls the properties of the window, such as specifying whether the window is an application window, a system window, or a child window, and setting its display level.
Examples using WindowManager
The following sample code demonstrates how to create a simple floating window using WindowManager:
// Get an instance of WindowManagerWindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); // Create a LayoutParams object to set the properties of the window params = new ( .WRAP_CONTENT, .WRAP_CONTENT, .TYPE_APPLICATION_OVERLAY, .FLAG_NOT_FOCUSABLE, ); // Create a view of a floating windowView myWindowView = (this).inflate(.my_window_layout, null); // Add views to window using WindowManager(myWindowView, params);
DecorView
Window is a container that contains the entire user interface, including all Views and ViewGroups. It also includes a DecorView, which is the root view of the Activity interface, responsible for containing the application's content view and other elements (such as title bar, status bar, etc.). DecorView plays an important role in Android applications.
The role of DecorView
DecorView has the following key roles in Android:
Accept content view: DecorView is a ViewGroup that contains the content view of the application. Content view is a developer-defined user interface layout, including buttons, text boxes, images and other elements. DecorView displays the application's user interface by adding a content view to itself.
Title bar and status bar: DecorView also includes elements such as title bar and status bar. The title bar usually contains the title and action buttons for the application (such as the return button). The status bar is located at the top of the screen and usually includes information such as system notifications, time and battery status.
The overall container of the user interface: DecorView acts as a container for the entire Activity interface, combining elements such as content view, title bar, and status bar to form a complete user interface.
Example using DecorView
Here is a sample code that demonstrates how to get a DecorView in Activity and change its background color:
// Get the DecorView of the current activityView decorView = getWindow().getDecorView(); // Change the background color of the DecorView();
In the above code, we first get the DecorView of the current Activity, and then usesetBackgroundColor
Method changes its background color to blue. This makes the background of the entire Activity turn blue.
Types of Window
There are different types of windows in Android, and each type of window has its specific uses and properties. Here are the details about the different window types
Application Windows
use: The application window is a basic component of the normal application interface and is used to display the application's user interface, such as activities and dialog boxes.
property: The application window can include a title bar, a content view, and a system status bar. They usually get focus and can interact with users.
// Create an application window in Activitypublic class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); } }
Sub-Windows
use: A child window is part of an application window and is usually used to display a part of a specific function or content, such as pop-up menus, dialog boxes, floating windows, etc.
property: The child window depends on its parent window and usually does not have its own title bar. They can be modal (preventing the user from interacting with other parts) or non-modal.
// Create a simple dialog in Activity builder = new (this); ("hint"); ("This is a dialog example."); ("Sure", new () { @Override public void onClick(DialogInterface dialog, int which) { // Handle the OK button click event } }); ("Cancel", new () { @Override public void onClick(DialogInterface dialog, which) { // Handle the cancel button click event } }); AlertDialog dialog = (); ();
System Windows
use: The system window is used to display system-level information, control interface elements and system notifications. They are not part of the application, but are managed by the Android system.
property: The system window includes status bar, navigation bar, lock screen, notification bar, etc. They are usually displayed on top of the application and have high system permissions.
Window that exists in special cases
In addition to the main types of windows mentioned above, there are also some windows in special cases, such as:
Toast window: Used to display short notification messages. They are a lightweight prompt box that usually requires no user interaction.
Floating window: A window that floats above other windows, usually used to display real-time information or widgets.
Keyboard window: Used to capture and process user keyboard input. The keyboard window is usually a system-level window, controlled by the input method manager.
System prompt box: Used to display system-level prompts, such as permission requests, application updates, etc.
Create a simple Window
First, let's create a simple Android Window that will contain a text view.
In the above code, we create a new Window and add a text view to its content view. This Window can be started as a standalone Activity.
Window features and logos
Each Window can have different features and flags, which can be set through the properties of the Window. For example, we can set Window to full screen mode, set window transparency, etc.
Lifecycle of Window and Activity
The life cycle of Window is closely related to the life cycle of Activity. When the Activity is created, the Window associated with it is created, and when the Activity is destroyed, its Window will also be destroyed. Let's learn more about how the Window's life cycle interacts with the Activity's life cycle.
Create Window
When you callsetContentView()
Or similar methods to set the content view of the Activity, the system will automatically create a Window for the Activity. This process usually occurs in the ActivityonCreate()
in the method.
@Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); // Create Window and set content view}
Show Window
Once the Window is created, the system processes its display. Usually, in the ActivityonStart()
andonResume()
In the method, the Window becomes visible and draws the user interface of the Activity on the screen.
@Override protected void onStart() { (); // Window becomes visible} @Override protected void onResume() { (); // Continue to draw Window}
Window interaction
The interaction between Window and users is usually in the ActivityonPause()
andonResume()
Occurs between methods. When a user interacts with a window (such as clicking a button), events are passed to the Window's content view.
@Override protected void onPause() { (); // Pause interaction with Window} @Override protected void onResume() { (); // Restore interaction with Window}
Destroy Window
When the activity is destroyed (for example, the user presses the return button or passesfinish()
When the method), the associated Window will also be destroyed. This process is usually in the ActivityonDestroy()
Completed in the method.
@Override protected void onDestroy() { (); // Destroy Window}
In short, the life cycle of the Window is closely related to the life cycle of the Activity, and the creation, display, interaction and destruction of the Window are all affected by the status of the Activity.
Customize Window
Finally, let's briefly introduce how to create a custom Window. Custom Window allows you to have full control over the application's UI for specific interface effects.
public class MyCustomWindow extends Window { public MyCustomWindow(Context context) { super(context); // Initialize in the constructor // Set the Window's features, layout, etc. } @Override public void draw(Canvas canvas) { // Draw custom UI elements here } // Other methods and logic for customizing Window}
By extending the Window class and implementing your own logic, you can create custom Window to meet the needs of your application.
in conclusion
This article introduces the core knowledge points of the Android window system in depth, including the type of Window, the creation of Window, the features and logos of Window, the life cycle of Window, and how to create custom windows. I hope that through this article, I can better understand and use the Android window system, so as to build a richer and more interactive Android application. Window is the basis of the Android application user interface, and mastering these concepts is crucial for Android development.
The above is a detailed article that will help you understand the Android Window system in depth. For more information about Android Window system, please follow my other related articles!