Activity的启动流程

sancaiodm Android源码摘录 2021-09-26 1460 0

Android11 展讯平台的Activity 启动流程

 ---Activity oncreate------
 java.lang.Exception
 	at android.app.Activity.onCreate(Activity.java:1594)
 	at androidx.core.app.ComponentActivity.onCreate(ComponentActivity.java:85)
 	at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:282)
 	at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:240)
 	at com.android.settings.core.SettingsBaseActivity.onCreate(SettingsBaseActivity.java:65)
 	at com.android.settings.SettingsActivity.onCreate(SettingsActivity.java:254)
 	at android.app.Activity.performCreate(Activity.java:8072)
 	at android.app.Activity.performCreate(Activity.java:8055)
 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3470)
 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3678)
 	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
 	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2106)
 	at android.os.Handler.dispatchMessage(Handler.java:106)
 	at android.os.Looper.loop(Looper.java:223)
 	at android.app.ActivityThread.main(ActivityThread.java:7942)
 	at java.lang.reflect.Method.invoke(Native Method)
 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:948)
以上是执行Activity oncreate方法的步骤

而在执行onStart方法或是Restart方法时稍微有一点区别
 tId:-1573709730 Transitioning activity: .uioverrides.QuickstepLauncher to state: ON_RESTART
 ----performLifecycleSequence-----
 java.lang.Exception
 	at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:218)
	at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
 	at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:183)
 	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:132)
 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
 	at android.os.Handler.dispatchMessage(Handler.java:106)
 	at android.os.Looper.loop(Looper.java:223)
 	at android.app.ActivityThread.main(ActivityThread.java:7944)
 	at java.lang.reflect.Method.invoke(Native Method)
 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:603)
 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 tId:-1573709730 Transitioning activity: .uioverrides.QuickstepLauncher to state: ON_START
 ----performLifecycleSequence-----
 java.lang.Exception
 	at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:218)
 	at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
 	at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:183)
 	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:132)
 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
 	at android.os.Handler.dispatchMessage(Handler.java:106)
 	at android.os.Looper.loop(Looper.java:223)
	at android.app.ActivityThread.main(ActivityThread.java:7944)
	at java.lang.reflect.Method.invoke(Native Method)
 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:603)
 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)


而执行oncreate onpause onstop  resume方法都差不多,只是具体实现策略不同,
PauseActivityItem,LaunchActivityItem,ResumeActivityItem,StopActivityItem
  ----ActivityThread::handlePauseActivity-----
  java.lang.Exception
  	at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4754)
  	at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:46)
  	at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
  	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
  	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2106)
  	at android.os.Handler.dispatchMessage(Handler.java:106)
  	at android.os.Looper.loop(Looper.java:223)
  	at android.app.ActivityThread.main(ActivityThread.java:7951)
  	at java.lang.reflect.Method.invoke(Native Method)
  	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:603)
  	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 ----ActivityThread::handleLaunchActivity-----
 java.lang.Exception
 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3648)
 	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
 	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2106)
 	at android.os.Handler.dispatchMessage(Handler.java:106)
 	at android.os.Looper.loop(Looper.java:223)
 	at android.app.ActivityThread.main(ActivityThread.java:7951)
 	at java.lang.reflect.Method.invoke(Native Method)
 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:603)
 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 ----ActivityThread::handleResumeActivity-----
 java.lang.Exception
 	at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4570)
 	at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
 	at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2106)
 	at android.os.Handler.dispatchMessage(Handler.java:106)
 	at android.os.Looper.loop(Looper.java:223)
 	at android.app.ActivityThread.main(ActivityThread.java:7951)
 	at java.lang.reflect.Method.invoke(Native Method)
 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:603)
 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 ----ActivityThread::handleStopActivity-----
 java.lang.Exception
 	at android.app.ActivityThread.handleStopActivity(ActivityThread.java:5020)
 	at android.app.servertransaction.StopActivityItem.execute(StopActivityItem.java:40)
 	at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2106)
 	at android.os.Handler.dispatchMessage(Handler.java:106)
 	at android.os.Looper.loop(Looper.java:223)
 	at android.app.ActivityThread.main(ActivityThread.java:7951)
 	at java.lang.reflect.Method.invoke(Native Method)
 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:603)
 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

AOSP/frameworks/base/core/java/android/app/ClientTransactionHandler.java
09-28 10:54:27.237  6707  7391 I zcailiu : ClientTransactionHandler:: scheduleTransaction
09-28 10:54:27.237  6707  7391 I zcailiu : java.lang.Exception
09-28 10:54:27.237  6707  7391 I zcailiu : 	at android.app.ClientTransactionHandler.scheduleTransaction(ClientTransactionHandler.java:48)
09-28 10:54:27.237  6707  7391 I zcailiu : 	at android.app.ActivityThread$ApplicationThread.scheduleTransaction(ActivityThread.java:1750)
09-28 10:54:27.237  6707  7391 I zcailiu : 	at android.app.IApplicationThread$Stub.onTransact(IApplicationThread.java:1275)
09-28 10:54:27.237  6707  7391 I zcailiu : 	at android.os.Binder.execTransactInternal(Binder.java:1159)
09-28 10:54:27.237  6707  7391 I zcailiu : 	at android.os.Binder.execTransact(Binder.java:1123)


ActivityStackSuperVesion.java中

image.png

image.png



Android 

1 Activity.java :: startActivity(Intent intent)  ---> startActivity(intent, null) -----> startActivityForResult(intent, -1) ----> startActivityForResult(intent, requestCode, null)

Instrumentation.java :: execStartActivity()

ActivityTaskManagerService.java ::startActivity  ---> startActivityAsUser --->  startActivityAsUser

ActivityStarter.java :: execute()---- >executeRequest---> startActivityUnchecked ------> startActivityInner

RootWindowContainer.java :: resumeFocusedStacksTopActivities() 

ActivityStack.java :: resumeTopActivityUncheckedLocked --------> resumeTopActivityInnerLocked


Activity.startActivity => startActivityForResult

Instrumentation.execStartActivity

ActivityTaskManager.startActivity

ActivityTaskManagerService.startActivity => startActivityAsUser

ActivityStarter.execute => executeRequest => startActivityUnchecked => startActivityInner

RootWindowContainer.resumeFocusedStacksTopActivities

ActivityStack.resumeTopActivityUncheckedLocked => resumeTopActivityInnerLocked

ClientLifecycleManager.scheduleTransaction

ClientTransaction.schedule

ActivityThread.ApplicationThread.scheduleTransaction

ActivityThread.scheduleTransaction(ActivityThread父类ClientTransactionHandler的方法)

ActivityThread.H.EXECUTE_TRANSACTION

//这一部分应该有误的,该 博主写的这此流程是onstart 与restart方法的执行流程 且又不全对,并非oncreate的流程

TransactionExecutor.execute => executeCallbacks => cycleToPath => performLifecycleSequence

//oncreate的执行流程应该是:

TransactionExecutor.execute => executeCallbacks =>LaunchActivityItem.execute()

ActivityThread.handleLaunchActivity => performLaunchActivity

Instrumentation.newActivity => callActivityOnCreate

Activity.performCreate => onCreate

————————————————

版权声明:本文为CSDN博主「guangdeshishe」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/guangdeshishe/article/details/114794116


activity的生命周期状态的切换位置 rameworks/base/core/java/android/app/servertransaction/TransactionExecutor.java

private void performLifecycleSequence(ActivityClientRecord r, IntArray path,
            ClientTransaction transaction) {
        final int size = path.size();
        for (int i = 0, state; i < size; i++) {
            state = path.get(i);
            if (DEBUG_RESOLVER) {
                Slog.d(TAG, tId(transaction) + "Transitioning activity: "
                        + getShortActivityName(r.token, mTransactionHandler)
                        + " to state: " + getStateName(state));
            }
            switch (state) {
                case ON_CREATE:
                    mTransactionHandler.handleLaunchActivity(r, mPendingActions,
                            null /* customIntent */);
                    break;
                case ON_START:
                    mTransactionHandler.handleStartActivity(r.token, mPendingActions);
                    break;
                case ON_RESUME:
                    mTransactionHandler.handleResumeActivity(r.token, false /* finalStateRequest */,
                            r.isForward, "LIFECYCLER_RESUME_ACTIVITY");
                    break;
                case ON_PAUSE:
                    mTransactionHandler.handlePauseActivity(r.token, false /* finished */,
                            false /* userLeaving */, 0 /* configChanges */, mPendingActions,
                            "LIFECYCLER_PAUSE_ACTIVITY");
                    break;
                case ON_STOP:
                    mTransactionHandler.handleStopActivity(r.token, 0 /* configChanges */,
                            mPendingActions, false /* finalStateRequest */,
                            "LIFECYCLER_STOP_ACTIVITY");
                    break;
                case ON_DESTROY:
                    mTransactionHandler.handleDestroyActivity(r.token, false /* finishing */,
                            0 /* configChanges */, false /* getNonConfigInstance */,
                            "performLifecycleSequence. cycling to:" + path.get(size - 1));
                    break;
                case ON_RESTART:
                    mTransactionHandler.performRestartActivity(r.token, false /* start */);
                    break;
                default:
                    throw new IllegalArgumentException("Unexpected lifecycle state: " + state);
            }
        }
    }


hjk@hjk-optiplex:~x/frameworks/base$ grep --include="*.java" "extends ClientTransactionItem" -srn .
grep: warning: GREP_OPTIONS is deprecated; please use an alias or script
./core/java/android/app/servertransaction/LaunchActivityItem.java:48:public class LaunchActivityItem extends ClientTransactionItem {
./core/java/android/app/servertransaction/ConfigurationChangeItem.java:30:public class ConfigurationChangeItem extends ClientTransactionItem {
./core/java/android/app/servertransaction/NewIntentItem.java:38:public class NewIntentItem extends ClientTransactionItem {
./core/java/android/app/servertransaction/EnterPipRequestedItem.java:27:public final class EnterPipRequestedItem extends ClientTransactionItem {
./core/java/android/app/servertransaction/ActivityRelaunchItem.java:39:public class ActivityRelaunchItem extends ClientTransactionItem {
./core/java/android/app/servertransaction/ActivityResultItem.java:36:public class ActivityResultItem extends ClientTransactionItem {
./core/java/android/app/servertransaction/FixedRotationAdjustmentsItem.java:30:public class FixedRotationAdjustmentsItem extends ClientTransactionItem {
./core/java/android/app/servertransaction/TopResumedActivityChangeItem.java:31:public class TopResumedActivityChangeItem extends ClientTransactionItem {
./core/java/android/app/servertransaction/ActivityLifecycleItem.java:28:public abstract class ActivityLifecycleItem extends ClientTransactionItem {
./core/java/android/app/servertransaction/ActivityConfigurationChangeItem.java:35:public class ActivityConfigurationChangeItem extends ClientTransactionItem {

hjk@hjk-optiplex:~/x/frameworks/base$ grep --include="*.java" "extends ActivityLifecycleItem" -srn .
/core/java/android/app/servertransaction/ResumeActivityItem.java:33:public class ResumeActivityItem extends ActivityLifecycleItem {
./core/java/android/app/servertransaction/StopActivityItem.java:30:public class StopActivityItem extends ActivityLifecycleItem {
./core/java/android/app/servertransaction/PauseActivityItem.java:33:public class PauseActivityItem extends ActivityLifecycleItem {
./core/java/android/app/servertransaction/StartActivityItem.java:30:public class StartActivityItem extends ActivityLifecycleItem {
./core/java/android/app/servertransaction/DestroyActivityItem.java:30:public class DestroyActivityItem extends ActivityLifecycleItem {



https://www.jianshu.com/p/e17b9851973f?tdsourcetag=s_pcqq_aiomsg

评论