Activity启动大致流程

Activity启动大致流程

这里写图片描述

public static void main(String[] args) {

        ......



        Looper.prepareMainLooper();

//实例化,同时里面引用有个ApplicationThread,是个binder,也同时实例化
        ActivityThread thread = new ActivityThread();

/**将ApplicationThread这个binder实例交给AMS,该binder持有主线程
 *的一个Handle H,以便AMS可以调用,也就是由AMS系统进程去请求 
 *  Acticity的各个生命周期和状态 比如启动调用 
 * ApplicationThread.scheduleLaunchActivity,跨进程
 */
        thread.attach(false);



        if (sMainThreadHandler == null) {

            sMainThreadHandler = thread.getHandler();
        }

        ......


/**开启消息循环,里面messageQueue.next()会阻塞,阻塞时会释放cpu资 
 *源,底层实现。一有ApplicationThread传过来的消息就处理,不会使得线
 *程卡死在这个循环,因为一有请求都处理了。
 */       
        Looper.loop();

        throw new RuntimeException("Main thread loop unexpectedly exited");

    }

猜你喜欢

转载自blog.csdn.net/sinat_33878878/article/details/79769515