原文转载:https://www.cnblogs.com/tiger-wang-ms/p/6517048.html
首先要关注的就是preserviedWindow参数,这个参数就是上一段中提到的mPendingRevomeWindow变量,这个参数在什么时候会不为空呢?其实这里的逻辑是用来疾速重启acitivity的,比方你的一个activity曾经启动了,然而主题换了或者configuration变了,这里只须要从新加载资源和view,没必从新再执行DecorView的创立工作。
另一个要关注的就是mDecor变量,这个变量是DecorView类型的,如果这里没有初始化的话,则会在调用setContentView办法中new一个DecorView对象进去。DecorView对象继承自FrameLayout,所以他实质上还是一个view,只是对FrameLayout做了肯定的包装,例如增加了一些与window须要调用的办法setWindowBackground()、setWindowFrame()等。咱们晓得,acitivty界面的view是呈树状构造的,而mDecor变量在这里作为activity的界面的根view存在。这三个点关系就比方,PhoneWindow是一块手机电子屏,DecorView就是电子屏要显示的内容,Activity就是手机电子屏装置地位。
再来看创立PhonewWindow之后调用的setWindowManager()办法的逻辑,这段代码是在PhonewWindow.java的父类Window.java中代码如下。
public void setWindowManager(WindowManager wm, IBinder appToken, String appName,
boolean hardwareAccelerated) {
mAppToken = appToken;
mAppName = appName;
mHardwareAccelerated = hardwareAccelerated
|| SystemProperties.getBoolean(PROPERTY_HARDWARE_UI, false);
if (wm == null) {
wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
}
mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this);
}
对于wWindowManager变量,实际上这里是创立了一个WindowManagerImpl对象。首先是这种首先获取零碎服务的代理到wm上,而后强制转换为WindowManagerImpl调用createLocalWindowManager(),在createLocalWindowManager()理论是执行了一个new WindowManagerImpl()到办法来创立。对于这部分代码看了很久很困惑的一个点,就是为啥要弄个这么简单的逻辑,间接把下面加粗的代码改为new WindowManagerImpl(…),这养会有什么区别吗?如果有大虾看到这里,心愿能帮我解答。
在WindowManager中保留了对于单例对象WindowManagerGloble的援用,即mGlobal变量。此外,WindowManager.java实现了WindowManager又,而WindowManager继承自ViewManager接口,ViewManager接口办法如下方代码。
public interface ViewManager
{
/**
* Assign the passed LayoutParams to the passed View and add the view to the window.
* <p>Throws {@link android.view.WindowManager.BadTokenException} for certain programming
* errors, such as adding a second view to a window without removing the first view.
* <p>Throws {@link android.view.WindowManager.InvalidDisplayException} if the window is on a
* secondary {@link Display} and the specified display can't be found
* (see {@link android.app.Presentation}).
* @param view The view to be added to this window.
* @param params The LayoutParams to assign to view.
*/
public void addView(View view, ViewGroup.LayoutParams params);
public void updateViewLayout(View view, ViewGroup.LayoutParams params);
public void removeView(View view);
}
在WindowManager对于addView()、updateViewLayout()和removeView()的实现,都是调用到mGlobal变量对应的addView()、updateViewLayout()和removeView()办法来实现的。这里咱们
这样咱们剖析完activity以及对应的window对象的创立,回到performLauncerActivity()办法中Activity a = performLaunchActivity(r, customIntent)这一步骤,之后便回调activity办法的onCreate(),在onCreate()的setContentView办法会初始化DecorView,并依据传入参数加载布局,具体步骤在下一节介绍。
再回到最后的handlerLaunchActivity()办法中,通过调用performLauncerActivity()创立出一个Acitivty对象后,如果创立胜利则执行handleResumeActivity(),便执行到了Acitivity的onResume()办法,即是实现了acitivty的启动。
二、setContentView()流程
首先,咱们个别在onCreate()里调用setContentView()的办法。
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
这里理论调用到到中央是Acitivity.java类中的setContentView()办法,如下。
public void setContentView(@LayoutRes int layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
这里getWindow()返回的是Acitivity.java类中的mWindow变量,就是Activity创立时一起创立的PhoneWindow对象,进入到
@Override
public void setContentView(int layoutResID) {
if (mContentParent == null) {
installDecor();
} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
mContentParent.removeAllViews();//如果屡次调用setContentView则会执行removeAllView操作
}
if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) { //过渡动画机制相干
final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
getContext());
transitionTo(newScene);
} else {
mLayoutInflater.inflate(layoutResID, mContentParent);
}
mContentParent.requestApplyInsets();
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
cb.onContentChanged();
}
mContentParentExplicitlySet = true;
}
代码里波及到FEATURE_CONTENT_TRANSITIONS的属性,这里是Android的过渡动画相干机制,这里咱们不再开展详述。个别的Acitivty启动时,会进入mContentParent为null的逻辑,首先调用的是installDecor()办法,实现DecorView的创立工作;之后调用mLayoutInflater.inflate()办法将咱们传入的资源文件转换为view树,装载到mContentParent中。首先来看installDecor()代码。
private void installDecor() {
mForceDecorInstall = false;
if (mDecor == null) { //创立DecorView
mDecor = generateDecor(-1);
mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
mDecor.setIsRootNamespace(true);
if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
}
} else {
mDecor.setWindow(this);
}
if (mContentParent == null) {
mContentParent = generateLayout(mDecor);
...
}
在这个办法又两个次要步骤,首先是应用generateDecor()办法创立了DecorView对象,generateDecor()办法比较简单,次要就是执行new DecorView(context, featureId, this, getAttributes())办法,这里不再贴出代码;重点来看generateLayout()办法,这个办法生成的mContentParent是作为来咱们后续加载加载的用户的布局的父布局存在的。
protected ViewGroup generateLayout(DecorView decor) {
// Apply data from current theme.
//获取以后主题的相干属性
TypedArray a = getWindowStyle();
... //一大段的依据获取到到主题属性,解析保留到PhonwWindow的相干参数的变量中
int layoutResource;
int features = getLocalFeatures();
... //一大段依据PhoneWindow的设定好的属性(features和mIsFloating)的判断,为layoutResource进行赋值, //值能够为R.layout.screen_custom_title、R.layout.screen_action_bar等
mDecor.startChanging(); //将layoutRsourece值对应的布局文件加载到DecorView中
mDecor.onResourcesLoaded(mLayoutInflater, layoutResource);
//在加载给DecorView的布局文件中有一块id为ID_ANDROID_CONTENT的区域是用于用户显示本人布局的,也是setContextView传入的布局显示的中央 //这块区域会以ViewGroup的模式赋值给mContentParent变量,这个ViewGroup即是用户布局的父布局节点
ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
... //持续一大段的属性配置
mDecor.finishChanging();
return contentParent;
}
generateLayout办法理论就是解析出主题的相干属性,依据不同的主题款式的属性值抉择不同的布局文件设置到DecorView中(DecorView本事就是FrameLayout)。在view的树状构造下,DecorView即是整个Window显示的视图的根节点,在DecorView的子节点中又有一块id为ID_ANDROID_CONTENT的区域有一块区域作为mContentParent变量用于加载用户的布局,与mContentParent平级的视图有ActionBar视图和Title的视图。总结来说,installDecor()办法本质就是产生mDecor和mContentParent对象。在installDecor之后,会执行到mLayoutInflater.inflate(layoutResID, mContentParent)办法将用户传入的布局转化为view再退出到mContentParent上。这样就实现了setContentView()流程。
点击下方链接收费获取Android进阶材料:
https://shimo.im/docs/tXXKHgdjPYj6WT8d/
发表回复