Activity启动?(慎入)

作者: 咸鱼佬 | 来源:发表于2017-06-14 14:58 被阅读110次

    ActivityThread#main

    在ActivityThread中值得关注的成员变量

    //Binder本地对象,ams 与 应用程序进行通信的中介
    final ApplicationThread mAppThread = new ApplicationThread();
        
    final H mH = new H();
    private class H extends Handler......
    

    ActivityThread#attach

    ActivityManagerProxy#attachApplication

    将ApplicationThread 对象写入到Parcel对象的data中,然后ActivityManagerProxy的内部Binder对象mRemote向ActivityManagerService发送一个ATTACH_APPLICATION_TRANSACTION进程间通信请求

    上面的动作都是在Application 进程中执行的,下面的就会在ams中执行了

    ..
    ........
    ...........
    ..............
    .................
    Android系统源代码情景分析

    然后ams向我们的应用进程发送一个LAUNCH_ACTIVITY_TRANSACTION进程通信请求

    然后我们的应用进程的ApplicationThread的scheduleLaunchActivity方法处理这个请求

    ApplicationThread#scheduleLaunchActivity

    最终通过mH(Handler实例)发送Message

    ActivityThread#mH#handleMessage

    ActivityThread#handleLaunchActivity

    1. unscheduleGcIdler

      从主线程的MessageQueue中的IdleHandler list中移除 用于gc的IdleHandler

    2. WindowManagerGlobal.initialize();

      初始化wms,得到wms实例

    ActivityThread#performLaunchActivity

    1. ActivityInfo
        /**
         * Information you can retrieve about a particular application
         * activity or receiver. This corresponds to information collected
         * from the AndroidManifest.xml's <activity> and
         * <receiver> tags.
         */
    

    包含一个特殊activity/receiver 的一些信息,跟你在AndroidManifest文件写的是一样的
    就是描述启动Activity的一些信息咯

    1. ComponentName
        /**
         * Identifier for a specific application component
         * ({@link android.app.Activity}, {@link android.app.Service},
         * {@link android.content.BroadcastReceiver}, or
         * {@link android.content.ContentProvider}) that is available.  Two
         * pieces of information, encapsulated here, are required to identify
         * a component: the package (a String) it exists in, and the class (a String)
         * name inside of that package.
         * 
         */
    

    描述着Activity/BroadcastReceiver/ContentProvider/Service 其中一个的信息,分别是

        private final String mPackage;//描述包的路径   
        private final String mClass;//类名
    
    1. Instrumentation

      介绍Instrumentation

         public Activity newActivity(ClassLoader cl, String className,
            Intent intent)
            throws InstantiationException, IllegalAccessException,
            ClassNotFoundException {
                return (Activity)cl.loadClass(className).newInstance();
        }
    

    通过反射创建了一个Activity

    1. Application app = r.packageInfo.makeApplication(false, mInstrumentation);
      packageInfo 是一个LoadedApk的实例
      然后再这个方法里面

        ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);
           app = mActivityThread.mInstrumentation.newApplication(
                   cl, appClass, appContext);
      

      创建了一个全局的Context对象,还有一个Application对象
      在newApplication方法中调用了Application的attach方法,将Context对象赋值给ContextWrapper的成员变量mBase

         static public Application newApplication(Class<?> clazz, Context context)
            throws InstantiationException, IllegalAccessException, 
            ClassNotFoundException {
            Application app = (Application)clazz.newInstance();
            ........        
            return app;
        }   
    
    1. activity.attach
      我们直接分析attach方法

    2. mInstrumentation.callActivityOnCreate

        prePerformCreate(activity);
        activity.performCreate(icicle);
        postPerformCreate(activity);
        //------
        final void performCreate(Bundle icicle) {
            restoreHasCurrentPermissionRequest(icicle);
            onCreate(icicle);//常见?
            mActivityTransitionState.readState(icicle);
            performCreateCommon();
        }
    

    然后再回到ActivityThread#handleLaunchActivity中


    来到最后一个handleResumeActivity方法

    handleResumeActivity

    在handleResumeActivity中调用Activity的performResume方法,然后在performResumeActivity方法调用Activity的performRestart,在performRestart调用Activity的performStart,然后再performStart调用Activity的onStart方法。
    然后回到Activity的performResume方法中,再通过Instrumentation调用Activity的onResume方法
    然后回到 handleResumeActivity,在Activity调用onResume之后

    void makeVisible() {
        if (!mWindowAdded) {
            ViewManager wm = getWindowManager();
            wm.addView(mDecor, getWindow().getAttributes());
            mWindowAdded = true;
        }
        mDecor.setVisibility(View.VISIBLE);
    }
    

    然后我们才能看到我们的界面

    最后

    带有目的性去看才有意义

    相关文章

      网友评论

        本文标题:Activity启动?(慎入)

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