美文网首页Android
Activity的生命周期和绘制流程

Activity的生命周期和绘制流程

作者: Peakmain | 来源:发表于2019-02-27 17:31 被阅读0次

    我之前分析了Activity的启动流程,若不熟悉的可以看上篇文章App启动——Activity的启动流程
    由之前文章我们可以知道最终Activity进入ActivityThread并发送名为LAUNCH_ACTIVITY的消息,最终会走到handleMessage方法

        case LAUNCH_ACTIVITY: {
                        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
                        final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
    
                        r.packageInfo = getPackageInfoNoCheck(
                                r.activityInfo.applicationInfo, r.compatInfo);
                        handleLaunchActivity(r, null);
                        Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                    } break;
    

    handleLaunchActivity源码:这里后期还会回来分析

     private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
            Activity a = performLaunchActivity(r, customIntent);
    
            if (a != null) {
                r.createdConfig = new Configuration(mConfiguration);
                Bundle oldState = r.state;
               //2
                handleResumeActivity(r.token, false, r.isForward,
                        !r.activity.mFinished && !r.startsNotResumed);
    
                if (!r.activity.mFinished && r.startsNotResumed) {
                      try {
                        r.activity.mCalled = false;
                        mInstrumentation.callActivityOnPause(r.activity);
                       if (r.isPreHoneycomb()) {
                            r.state = oldState;
                        }
                     //代码省略
                    } catch (SuperNotCalledException e) {
                        throw e;
                    } catch (Exception e) {
                        //代码省略
                    }
                    r.paused = true;
                }
               //代码省略
            } 
        }
    

    performLaunchActivity源码上篇已经说过不再阐述,实际调用的是Activity的onCreate方法

    handleResumeActivity源码分析

    final void handleResumeActivity(...) {
            ActivityClientRecord r = performResumeActivity(token, clearHide);
        }
       public final ActivityClientRecord performResumeActivity(..){
           //代码省略
            r.activity.performResume();
      if (!r.activity.mFinished && r.startsNotResumed) {
                        try {
                        r.activity.mCalled = false;
                        mInstrumentation.callActivityOnPause(r.activity);
                      if (r.isPreHoneycomb()) {
                            r.state = oldState;
                        }
                    //代码省略
            }
    if (r.window == null && !a.mFinished && willBeVisible) {
                   //代码省略
                    if (a.mVisibleFromClient) {
                        a.mWindowAdded = true;
                        wm.addView(decor, l);
                    }
                }
    }
    

    r.activity.performResume实际调用的是Activity的Resume

    final void performResume() {
            performRestart();
       //代码省略
            // Now really resume, and install the current status bar and menu.
            mCalled = false;
       //代码省略
            mFragments.dispatchResume();
            mFragments.execPendingActions();
             //代码省略
          mInstrumentation.callActivityOnResume(this);
        }
    

    performRestart源码

    final void performRestart() {
        //代码省略
            if (mStopped) {//停止的状态
                mStopped = false;
          //代码省略
               mCalled = false;
                mInstrumentation.callActivityOnRestart(this);
                   performStart();
            }
        }
    
    

    mInstrumentation.callActivityOnRestart(this)实际调用的是activity的onRestart方法

    performStart()源码分析;

    final void performStart() {
    //代码省略
     mInstrumentation.callActivityOnStart(this);
    }
    

    实际调用的是Instrumentation中的callActivityOnStrart方法

       public void callActivityOnStart(Activity activity) {
            activity.onStart();
        }
    

    我们可以看到performStart实际调用的是onStart方法

    现在我们回到r.activity.performResume方法中,看 mInstrumentation.callActivityOnResume(this);

        public void callActivityOnResume(Activity activity) {
            activity.mResumed = true;
            activity.onResume();
            //代码省略
        }
    

    实际调用的是Activity的onResume方法

    我们这时候再回到handleResumeActivity中看 wm.addView(decor, l);的源码

    public interface ViewManager
    {
        public void addView(View view, ViewGroup.LayoutParams params);
        public void updateViewLayout(View view, ViewGroup.LayoutParams params);
        public void removeView(View view);
    }
    

    可以看到实际是个抽象类,我们看wm是怎么被实例化的

    //我们会发现这个方法会创建一个WindowManager
     ViewManager wm = a.getWindowManager();
    
     public WindowManager getWindowManager() {
            return mWindowManager;
        }
    

    我们都知道WindowManager的实现类是PhoneWindow,查看其getWindowManager源码,我们会发现,实际这个走的是Window中的getWindowManager()方法

    //实际最终创建的是WindowManagerImpl这个实例,查看这个源码中的addView方法
     mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this);
    
       @Override
        public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
            applyDefaultToken(params);
            mGlobal.addView(view, params, mContext.getDisplay(), mParentWindow);
        }
    

    WindowMangerImp中的addView源码分析

    ViewRootImpl root;
     root = new ViewRootImpl(view.getContext(), display);
      try { 
                //设置view进去
                    root.setView(view, wparams, panelParentView);
                } catch (RuntimeException e) {
                    // BadTokenException or InvalidDisplayException, clean up.
                    if (index >= 0) {
                        removeViewLocked(index, true);
                    }
                    throw e;
                }
    

    调用的ViewRootImpl中的setView方法,调用其中的 requestLayout()-> scheduleTraversals();->mTraversalRunnable->doTraversal->performMeasure->view的measure-.onMeasure方法这时候我们就看到了onMeasure实际是在onResume方法之后调用的

    相关文章

      网友评论

        本文标题:Activity的生命周期和绘制流程

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