Understanding the Android Window Mechanism: From PhoneWindow to ViewRootImpl
This article explains the Android Window mechanism in depth, covering the roles of PhoneWindow, WindowManagerImpl, DecorView, ViewRootImpl, and their interactions during Activity startup, view creation, and rendering, with detailed code examples and step‑by‑step analysis.
When discussing the Window mechanism, developers often think of PhoneWindow , ViewRootImpl , WindowManagerImpl , sub‑windows, and DecorView . This article systematically clarifies how to understand the Window mechanism by focusing on the Window class itself and gradually expanding to more complex components.
Key conclusions:
Each Window has a unique WindowManager that maintains its contents.
On Android, the concrete implementation of Window is PhoneWindow , whose manager is WindowManagerImpl .
WindowManagerImpl manages the DecorView and its child view tree.
An Activity has exactly one PhoneWindow instance; a Dialog also has its own PhoneWindow .
The creation flow is as follows:
During Activity launch, ActivityThread creates an Activity instance.
The Activity’s attach() method creates a PhoneWindow and a corresponding WindowManagerImpl .
In Activity#onCreate() , setContentView() creates a DecorView for the PhoneWindow .
During ActivityThread#handleResumeActivity() , WindowManagerImpl#addView is called, which creates a ViewRootImpl and binds it to the view tree.
The ViewRootImpl then coordinates layout, drawing, refresh, and event dispatch for the view tree.
Below are important code excerpts illustrating these steps:
//: Window.java
public abstract class Window {
...
private WindowManager mWindowManager;
...
public void setWindowManager(WindowManager wm, IBinder appToken, String appName,
boolean hardwareAccelerated) {
if (wm == null) {
wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
}
mWindowManager = ((WindowManagerImpl) wm).createLocalWindowManager(this);
}
} //: ActivityThread.java
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
...
activity.attach(appContext, this, getInstrumentation(), r.token,
r.ident, app, r.intent, r.activityInfo, title, r.parent,
r.embeddedID, r.lastNonConfigurationInstances, config,
r.referrer, r.voiceInteractor, window, r.configCallback,
r.assistToken);
...
} //: Activity.java
final void attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken) {
...
// create PhoneWindow object
mWindow = new PhoneWindow(this, window, activityConfigCallback);
...
// set WindowManager for PhoneWindow
mWindow.setWindowManager(
(WindowManager) context.getSystemService(Context.WINDOW_SERVICE),
mToken, mComponent.flattenToString(),
(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
mWindowManager = mWindow.getWindowManager();
...
} //: WindowManagerImpl.java
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
applyDefaultToken(params);
mGlobal.addView(view, params, mContext.getDisplay(), mParentWindow);
} //: WindowManagerGlobal.java
public void addView(View view, ViewGroup.LayoutParams params,
Display display, Window parentWindow) {
...
final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
ViewRootImpl root;
View panelParentView = null;
synchronized (mLock) {
// create ViewRootImpl instance
root = new ViewRootImpl(view.getContext(), display);
view.setLayoutParams(wparams);
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
// bind ViewRootImpl to the view tree
root.setView(view, wparams, panelParentView);
}
...
}PopupWindow and Dialog are also explained as examples of sub‑windows and independent windows, showing how they use WindowManagerImpl to add their own view trees.
In summary, both Activities and UI components like PopupWindow and Dialog ultimately display content by invoking WindowManagerImpl#addView , which creates a ViewRootImpl to bind the view tree and communicate with the WindowManagerService for rendering and event handling.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.