当前位置:网站首页>Source code analysis | activity setcontentview I don't flash
Source code analysis | activity setcontentview I don't flash
2022-06-23 02:14:00 【Petterp】
I know everyone hates reading source code analysis written by others , Because you can't stop talking at length , Don't talk about military morality , Is that appropriate , This is not appropriate . therefore , This is a different source code analysis , If you don't understand after reading .
introduction
An ordinary one Activity-setContentView(), Do you know what it does inside ?
Summary
Source code analysis
So let's see Activity-setContentView Method :
public void setContentView(@LayoutRes int layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}A simple way , Internal call getWindows.setContentView(xxx)
wait ,Windows What is it? ?
WindowsRepresents the concept of a window ,Android Whether it is Activity,dialog, still Toast Their views are attached to Windows On , Therefore, it can be called windows yes View The direct manager of . and Windows Only implementation classes , namely PhoneWindows.
Let's go on to see PhoneWindows Of setContentView()
@Override
public void setContentView(int layoutResID) {
if (mContentParent == null) {
installDecor(); // concerns
} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
mContentParent.removeAllViews();
}
mLayoutInflater.inflate(layoutResID, mContentParent);
}A simple , Internally executed a judgment , And then call **installDecor() ** Method .
wait ,mContenParent What is it? ?
mContentParent That is to put our own container layout , You can understand it as , It is our root layout , See the picture for details .
Let's go on to see installDecor() Method :
private void installDecor() {
... Ignore
mDecor = generateDecor(-1); // concerns 1
... Ignore
if (mContentParent == null) {
// concerns 2
mContentParent = generateLayout(mDecor);
}
... Ignore a large paragraph
}This method is cumbersome inside , It stinks for a long time , Do we need to pay so much attention , Unwanted , So go straight into generateDecor().
wait ,mDecor What is it? ?
mDecor yes
windowsUnique view , That's usmContentParentDad . abbreviation DecorView, Did you recall something .
Let's go on to see generateDecor() Method
protected DecorView generateDecor(int featureId) {
... Ignore a large paragraph
return new DecorView(context, featureId, this, getAttributes());
} wow , This method loves , direct new One. DecorView, Nothing else , Go back and see installDecor() The focus in 2-generateLayout().
We enter generateLayout() Method :
protected ViewGroup generateLayout(DecorView decor) {
//1
TypedArray a = getWindowStyle();
if(xx)else if(xxx)
else {
layoutResource = R.layout.screen_simple;
}
//2
mDecor.onResourcesLoaded(mLayoutInflater, layoutResource);
.. Ignore some
//3
ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
return contentParent;
}- Show get current theme , Then start judging which layout to use
- Load it into DecorView in
- adopt findViewById( The internal is DecorView.findViewById) obtain R.id.content, And back here viewGroup.
wait , This R.layout.screen_simple What is it ?
Oh , Just a common layout , Nothing too strange .
String ideas
Let's go through the above analysis as a whole :
- When we call Activity Of setContentView when , It's actually implemented internally
PhoneWindows(windows The only example of ) Of setContenView() ; - and
PhoneWindowsOf setContentView() The internal department will first determine whether there is a root layoutcontentParent, That is, whether there isDecorView, without , perform installDecor() To initialize ourDecorViewAndcontentParent; - stay installDecor() Method inside , Will first judge whether there is
DecorView, without , First new One came out , And then judge if there iscontentParent, If not , Just go according to the current theme , Select a layout , And add it as our root layout toDecorViewin ; - Last PhoneWindows-setContentView() Next, we can put our own layout inflate Into this root layout ;
边栏推荐
- Cmake passing related macros to source code
- Schedule tasks to periodically restart remote services or restart machines
- 1. Mx6u bare metal program (1) - Lighting master
- WebService details
- JS rotation chart (Netease cloud rotation chart)
- 2022-1-14
- You can be what you want to be
- C language foundation ----- write a function to find the larger value of two unequal integers
- OVS port traffic statistics practice
- Branch and loop statements (including goto statements) -part2
猜你喜欢

1. introduction to MySQL database connection pool function technology points

Cmake simple usage

Vs Code inadvertently disable error waveform curve

For Xiaobai who just learned to crawl, you can understand it after reading it

1. Mx6u bare metal program (6) - timer

Freshman C language summary post (hold change) Part1 output diamond

Anaconda creates a new environment encounter pit
![Buuctf misc-[actf freshman competition 2020]outline](/img/a4/ac9d14a69e0759d1e7c65740415bf7.jpg)
Buuctf misc-[actf freshman competition 2020]outline
![Buuctf misc-[bjdctf2020] Nani](/img/4e/ac6bf2f64cb68136581814da73db66.jpg)
Buuctf misc-[bjdctf2020] Nani

Freshman C language summary post (hold change) Part 2 formatted monthly calendar
随机推荐
2022-1-12
Special exercise split line-----------------------------
Garbled code of SecureCRT, double lines, double characters, unable to input (personal detection)
Initial structure
Ansible practice of Nepal graph
Unique in Pimpl_ PTR compilation errors and Solutions
Practice and exploration of vivo live broadcast application technology
5g core network and core network evolution
9. class and object practice and initialization list
8. destruct, construct, deep copy, shallow copy, assignment operator overload
The practice of traffic and data isolation in vivo Reviews
Uniapp View Horizontal Center
//1.7 use of escape characters
Mobile communication Overview - Architecture
Using mock data in vite projects -vite plugin mock
Interviewer: what is the difference between SSH and SSM frameworks? How to choose??
Stop automatically after MySQL starts (unable to start)
4. functions and inline functions with default values for formal parameters
Bubble sort - double for implementation
Primary pointer part