• Activity系列:自定义MainActivity的onCreate()在哪里被调用?


    本文查看的源码是sdk 29

    从ApplicationThread.scheduleTransaction()开始看起,至于为什么从这里开始看,后续的文章会讲到。这篇先分析onCreate()到底是在哪里被调用的。

    ApplicationThread.java

    public final class ActivityThread extends ClientTransactionHandler {
    ...
    private class ApplicationThread extends IApplicationThread.Stub {
    		...
            @Override
            public void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
                ActivityThread.this.scheduleTransaction(transaction);
            }
    		...
    }
    ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    ApplicationThread没有scheduleTransaction()这个方法,直接找他的父类ClientTransactionHandler.java 发送了一个msg.what为

    H是 ActivityThread的内部类,如下:

    public final class ActivityThread extends ClientTransactionHandler {
    	...
        class H extends Handler {
            public void handleMessage(Message msg) {
            ...
                switch (msg.what) {
                     case EXECUTE_TRANSACTION:
                        final ClientTransaction transaction = (ClientTransaction) msg.obj;
                        mTransactionExecutor.execute(transaction);
                      	...
                        break;           
                }        
            ...
            }    
        }
        ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    继续看,如下图:

    TransactionExecutor.java 中的execute()方法如下图:

    其中,关键代码:executeCallbacks(transaction),executeCallbacks()方法完整代码如下:
    TransactionExecutor.java

    
        /** Cycle through all states requested by callbacks and execute them at proper times. */
        @VisibleForTesting
        public void executeCallbacks(ClientTransaction transaction) {
            final List<ClientTransactionItem> callbacks = transaction.getCallbacks();
            if (callbacks == null || callbacks.isEmpty()) {
                // No callbacks to execute, return early.
                return;
            }
            if (DEBUG_RESOLVER) Slog.d(TAG, tId(transaction) + "Resolving callbacks in transaction");
    
            final IBinder token = transaction.getActivityToken();
            ActivityClientRecord r = mTransactionHandler.getActivityClient(token);
    
            // In case when post-execution state of the last callback matches the final state requested
            // for the activity in this transaction, we won't do the last transition here and do it when
            // moving to final state instead (because it may contain additional parameters from server).
            final ActivityLifecycleItem finalStateRequest = transaction.getLifecycleStateRequest();
            final int finalState = finalStateRequest != null ? finalStateRequest.getTargetState()
                    : UNDEFINED;
            // Index of the last callback that requests some post-execution state.
            final int lastCallbackRequestingState = lastCallbackRequestingState(transaction);
    
            final int size = callbacks.size();
            for (int i = 0; i < size; ++i) {
                final ClientTransactionItem item = callbacks.get(i);
                if (DEBUG_RESOLVER) Slog.d(TAG, tId(transaction) + "Resolving callback: " + item);
                final int postExecutionState = item.getPostExecutionState();
                final int closestPreExecutionState = mHelper.getClosestPreExecutionState(r,
                        item.getPostExecutionState());
                if (closestPreExecutionState != UNDEFINED) {
                    cycleToPath(r, closestPreExecutionState, transaction);
                }
    
                item.execute(mTransactionHandler, token, mPendingActions);
                item.postExecute(mTransactionHandler, token, mPendingActions);
                if (r == null) {
                    // Launch activity request will create an activity record.
                    r = mTransactionHandler.getActivityClient(token);
                }
    
                if (postExecutionState != UNDEFINED && r != null) {
                    // Skip the very last transition and perform it by explicit state request instead.
                    final boolean shouldExcludeLastTransition =
                            i == lastCallbackRequestingState && finalState == postExecutionState;
                    cycleToPath(r, postExecutionState, shouldExcludeLastTransition, transaction);
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    关键代码:

    item.execute(mTransactionHandler, token, mPendingActions);
    
    • 1

    item的类型是ClientTransactionItem,ClientTransactionItem是LaunchActivityItem的父类:

    在这里插入图片描述

    ActivityThread继承自ClientTransactionHandler,如下:

    public final class ActivityThread extends ClientTransactionHandler {
       
    }  
    
    • 1
    • 2
    • 3

    所以,直接看ActivityThread.handleLaunchActivity()方法:

        @Override
        public Activity handleLaunchActivity(ActivityClientRecord r,
                PendingTransactionActions pendingActions, Intent customIntent) {
            // If we are getting ready to gc after going to the background, well
            // we are back active so skip it.
            unscheduleGcIdler();
            mSomeActivitiesChanged = true;
    
            if (r.profilerInfo != null) {
                mProfiler.setProfiler(r.profilerInfo);
                mProfiler.startProfiling();
            }
    
            // Make sure we are running with the most recent config.
            handleConfigurationChanged(null, null);
    
            if (localLOGV) Slog.v(
                TAG, "Handling launch of " + r);
    
            // Initialize before creating the activity
            if (!ThreadedRenderer.sRendererDisabled
                    && (r.activityInfo.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
                HardwareRenderer.preload();
            }
            WindowManagerGlobal.initialize();
    
            // Hint the GraphicsEnvironment that an activity is launching on the process.
            GraphicsEnvironment.hintActivityLaunch();
    		
    		//#########在这里调用了performLaunchActivity()##############/
            final Activity a = performLaunchActivity(r, customIntent);
    
            if (a != null) {
                r.createdConfig = new Configuration(mConfiguration);
                reportSizeConfigurations(r);
                if (!r.activity.mFinished && pendingActions != null) {
                    pendingActions.setOldState(r.state);
                    pendingActions.setRestoreInstanceState(true);
                    pendingActions.setCallOnPostCreate(true);
                }
            } else {
                // If there was an error, for any reason, tell the activity manager to stop us.
                try {
                    ActivityTaskManager.getService()
                            .finishActivity(r.token, Activity.RESULT_CANCELED, null,
                                    Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
                } catch (RemoteException ex) {
                    throw ex.rethrowFromSystemServer();
                }
            }
    
            return a;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    继续跟ActivityThread.performLaunchActivity()方法:

     /**  Core implementation of activity launch. */
        private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    		...
    
            try {
                ...
                if (activity != null) {
    				...
                    activity.mCalled = false;
                    if (r.isPersistable()) {
                        mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
                    } else {
                        mInstrumentation.callActivityOnCreate(activity, r.state);
                    }
                   ...
                }
              
               ...
    
            } catch (SuperNotCalledException e) {
                throw e;
    
            } catch (Exception e) {
               ...
            }
    
            return activity;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    Instrumentation.java

    终于进入Activity.java了:

  • 相关阅读:
    公开可用的API 合集
    PAT甲级--1035 Password
    aarch64 python3.6.8 pip安装cinder 18.2.1
    宏macro
    【Qt】之【项目】整理可参考学习的git项目链接(持续更新)
    【简单模拟添加并合并通讯录~python+】
    签名墙互动项目分析
    MCE | 神经退行性疾病——比癌症更残忍
    深入Go语言:进阶指南
    表单验证及更改页面背景图片
  • 原文地址:https://blog.csdn.net/zhangjin1120/article/details/126454525