当前位置:网站首页>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? ?

Windows Represents 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 windows Unique view , That's us mContentParent Dad . 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;
}
  1. Show get current theme , Then start judging which layout to use
  2. Load it into DecorView in
  3. 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 PhoneWindows Of setContentView() The internal department will first determine whether there is a root layout contentParent, That is, whether there is DecorView, without , perform installDecor() To initialize our DecorView And contentParent;
  • stay installDecor() Method inside , Will first judge whether there is DecorView, without , First new One came out , And then judge if there is contentParent, If not , Just go according to the current theme , Select a layout , And add it as our root layout to DecorView in ;
  • Last PhoneWindows-setContentView() Next, we can put our own layout inflate Into this root layout ;
原网站

版权声明
本文为[Petterp]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202091438480890.html