current position:Home>Analyze the relationship between activity and window and view from the perspective of source code

Analyze the relationship between activity and window and view from the perspective of source code

2022-01-26 23:41:29 Random code 3000

This is my participation 8 The fourth of the yuegengwen challenge 16 God , Check out the activity details :8 Yuegengwen challenge

We all know that the loading of layout files is in Activity Of onCreate() In the method , Use setContentView Loading

This method is an overloaded method

They are all used without exception getWindow() Loading

that window When was it created ?

We know Acitivity The life cycle of is from onCreate At the beginning , In fact, there is another method that has been executed before it , That's it attach Method

PolicyManager Created a new Window object


We enter PolicyManager Class

IPolicy It's an interface

We need to find its implementation class , Find by looking Realized IPolicy Interface

The implementation code is as follows :

I just created one PhoneWindow object ,

That means For every one created Activity Will create a PhoneWindow object

that PhoneWindow And Window What is the relationship ?

PhoneWindow yes Window Subclasses of

So that's it , We know, actually Activity Medium setContentView It's actually PhoneWindow Processing

We find Can find the corresponding method

We overload the three methods to analyze

First, first

We usually develop in xml The layout written in is not the root node , It is contentParent The son of view


The relationship between them can be roughly understood as :

Activity: Equivalent to a house

Window: Equivalent to a window in the house

View: Equivalent to a window flower on the window

The order in which they are created is :


The specific sequence diagram is as follows :

copyright notice
author[Random code 3000],Please bring the original link to reprint, thank you.

Random recommended