当前位置:网站首页>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 ;
边栏推荐
- This monitoring tool is enough for the operation and maintenance of small and medium-sized enterprises - wgcloud
- 1.3-1.4 web page data capture
- Three methods for solving Fibonacci sequence feibonacci (seeking rabbit) - program design
- C language game minesweeping [simple implementation]
- "Initial C language" (Part 1)
- Digital circuit logic design
- JS request path console reports an error failed to launch 'xxx' because the scheme does not have a registered handler
- Analog Electronic Technology
- Array part
- Constexpr keyword
猜你喜欢

Understand GB, gbdt and xgboost step by step

Docker installs mysql5.7 and mounts the configuration file

Data analysis method - user group analysis

Microservice Optimization: internal communication of microservices using grpc

5g access network and base station evolution

Dynamic address book in C language (add, delete, modify, check (duplicate), delete, sort and export)

Li Mu's notes on machine learning -1.2

9. class and object practice and initialization list

Three methods for solving Fibonacci sequence feibonacci (seeking rabbit) - program design

Quick sorting C language code + auxiliary diagram + Notes
随机推荐
【CodeWars】What is between?
Pychart installation instructions
Common mistakes in C language (sizeof and strlen)
Cmake configuration error, error configuration process, Preject files may be invalid
Byte order: big endian vs little endian
Detailed explanation of makefile usage
Browser independent way to detect when image has been loaded
Array part
1.3-1.4 web page data capture
JS advanced part
Targeted and ready to go
5g access network and base station evolution
This monitoring tool is enough for the operation and maintenance of small and medium-sized enterprises - wgcloud
//1.15 putchar function
Dynamic address book in C language (add, delete, modify, check (duplicate), delete, sort and export)
//1.17 printf function
2021-11-11
1. Mx6u bare metal program (5) - external interrupt
OVS port traffic statistics practice
About the use of mock framework