首先要关注的就是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
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
这里实际调用到到地方是Acitivity.java类中的setContentView()方法,如下。
publicvoidsetContentView(@LayoutResint layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
这里getWindow()返回的是Acitivity.java类中的mWindow变量,就是Activity创建时一起创建的PhoneWindow对象,进入到
@Override
publicvoidsetContentView(int layoutResID) {if(mContentParent ==null) {installDecor();}elseif(!hasFeature(FEATURE_CONTENT_TRANSITIONS)) { mContentParent.removeAllViews();//如果多次调用setContentView则会执行removeAllView操作}if(hasFeature(FEATURE_CONTENT_TRANSITIONS)) {//过渡动画机制相关
finalScene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
getContext());
transitionTo(newScene);
} else{mLayoutInflater.inflate(layoutResID, mContentParent);} mContentParent.requestApplyInsets();finalCallback cb = getCallback();
if(cb !=null&& !isDestroyed()) {
cb.onContentChanged();
}
mContentParentExplicitlySet =true;
}
代码里涉及到FEATURE_CONTENT_TRANSITIONS的属性,这里是Android的过渡动画相关机制,这里我们不再展开详述。一般的Acitivty启动时,会进入mContentParent为null的逻辑,首先调用的是installDecor()方法,完成DecorView的创建工作;之后调用mLayoutInflater.inflate()方法将我们传入的资源文件转换为view树,装载到mContentParent中。首先来看installDecor()代码。
privatevoid installDecor() {
mForceDecorInstall =false;
if(mDecor ==null) {//创建DecorViewmDecor= 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()流程。