我们都知道 View 不能单独存在,必须依附在 Window 上面,因此有视图的地方就有 Window。这些视图包括 :Activity,Dialog,Toast,PopupWindow 等等。
一、Window创建流程
1、Activity的Window创建过程
在启动一个新的 Activity 的时候,最终都会调用 performLaunchActivity() 方法来完成整个启动过程。在这里会通过类加载器创建 Activity 的实例对象,并调用其 attach 方法为其关联所需的环境变量。
ActivityThread
源码位置:/frameworks/base/core/java/android/app/ActivityThread.java
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
……
try {
……
if (activity != null) {
……
appContext.setOuterContext(activity);
activity.attach(appContext, this, getInstrumentation(), r.token,