美文网首页
Activity-创建

Activity-创建

作者: lvcs | 来源:发表于2019-08-23 11:24 被阅读0次

    在栈顶Activity执行onPause方法退出后,开始新建Activity。
    如果APP是首次启动则让Zygote进程fork出一个新的进程,并根据传递的”android.app.ActivityThread”字符串,反射出该对象并执行ActivityThread的main方法对其进行初始化。

    1.消息循环的创建

    //ActivityThread
    public static void main(String[] args) {
    ......
            //创建Looper和MessageQueue对象,用于处理主线程的消息
            Looper.prepareMainLooper();
            //创建ActivityThread对象
            ActivityThread thread = new ActivityThread(); 
            //建立Binder通道 (创建新线程ApplicationThread)
            thread.attach(false, startSeq);
            Looper.loop(); //消息循环开始运行
            throw new RuntimeException("Main thread loop unexpectedly exited");
    }
    

    2.Application的创建

    //ActivityThread
    final ApplicationThread mAppThread = new ApplicationThread();
    private class ApplicationThread extends IApplicationThread.Stub {......}
    
    private void attach(boolean system, long startSeq) {
    ......
       final IActivityManager mgr = ActivityManager.getService();
                try {
                    //ActivityManagerService初始化Application相关信息
                    mgr.attachApplication(mAppThread, startSeq);
                } catch (RemoteException ex) {
                    throw ex.rethrowFromSystemServer();
                }
    ......
    }
    
    //ActivityManagerService
    public final void attachApplication(IApplicationThread thread, long startSeq) {
            synchronized (this) {
    ......
                attachApplicationLocked(thread, callingPid, callingUid, startSeq);
    ......
            }
    } 
    
    
    private final boolean attachApplicationLocked(IApplicationThread thread,
                int pid, int callingUid, long startSeq) {
    ......
        //通知ActivityThread启动application(IApplicationThread是ActivityThread的内部类,负责与 ActivityManagerService通讯)
        thread.bindApplication(processName, appInfo, providers, null, profilerInfo,
                            null, null, null, testMode,
                            mBinderTransactionTrackingEnabled, enableTrackAllocation,
                            isRestrictedBackupMode || !normalMode, app.persistent,
                            new Configuration(getGlobalConfiguration()), app.compat,
                            getCommonServicesLocked(app.isolated),
                            mCoreSettingsObserver.getCoreSettingsLocked(),
                            buildSerial, isAutofillCompatEnabled);
    ......
    }
    
    //ActivityThread
    public final void bindApplication(String processName, ApplicationInfo appInfo,
                    List<ProviderInfo> providers, ComponentName instrumentationName,
                    ProfilerInfo profilerInfo, Bundle instrumentationArgs,
                    IInstrumentationWatcher instrumentationWatcher,
                    IUiAutomationConnection instrumentationUiConnection, int debugMode,
                    boolean enableBinderTracking, boolean trackAllocation,
                    boolean isRestrictedBackupMode, boolean persistent, Configuration config,
                    CompatibilityInfo compatInfo, Map services, Bundle coreSettings,
                    String buildSerial, boolean autofillCompatibilityEnabled) {
                //设置相关数据
    ......
                 //发送消息
                sendMessage(H.BIND_APPLICATION, data);
    }
    
    public void handleMessage(Message msg) {
                switch (msg.what) {
                    case BIND_APPLICATION:
                        AppBindData data = (AppBindData)msg.obj;
                        handleBindApplication(data);
    ......
            }
    }
    
    private void handleBindApplication(AppBindData data) {
    ......
           //创建上下文对象
           final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
           Application app;
           //初始化Applcation类(如果该进程存在直接返回,否则新建一个 最终通过反射的方式创建了一个Application对象)
           app = data.info.makeApplication(data.restrictedBackupMode, null);
           //通过Instrumentation对象调用lApplication的OnCreate方法
           mInstrumentation.callApplicationOnCreate(app);
    ......
    }
    
    

    3.Activity的创建

    //ActivityThread
    private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
            //初始化上下文环境
            ContextImpl appContext = createBaseContextForActivity(r);
            Activity activity = null;
            java.lang.ClassLoader cl = appContext.getClassLoader();
            //创建Activity 
            activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent);
    ......
            //初始化Application(如果Application存在直接返回原有的否则创建新的)
            Application app = r.packageInfo.makeApplication(false, mInstrumentation);
    ......          
            //设置Activity的上下文环境
            appContext.setOuterContext(activity);
            //调用activity的attach方法,在其中创建PhoneWindow对象
            activity.attach(appContext, this, getInstrumentation(), r.token,
            r.ident, app, r.intent, r.activityInfo, title, r.parent,
            r.embeddedID, r.lastNonConfigurationInstances, config,
            r.referrer, r.voiceInteractor, window, r.configCallback);
    ......    
           // 设置Activity的Theme
           int theme = r.activityInfo.getThemeResource();
           if (theme != 0) {
                   activity.setTheme(theme);
            }
    ......         
           //通过Instrumentation调用activity的onCreate方法
           if (r.isPersistable()) {
           mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
           } else {
           mInstrumentation.callActivityOnCreate(activity, r.state);
          }
    }
    

    相关文章

      网友评论

          本文标题:Activity-创建

          本文链接:https://www.haomeiwen.com/subject/okhzsctx.html