Activity 启动流程log分析

发布时间:2024年01月13日

为了方便跟踪启动过程相关流程,打开debug开关,并添加Log打印

1.Log打点

1)打开debug开关

打开 WindowManagerDebugConfig开关,会打开InputMonitor?InputManagerCallback?PhoneWindowManager中的input向相关日志打印

public static boolean DEBUG_INPUT = true;

打开Activity中的debug开关,可以直接看到activity生命周期变化

?? ?private static final boolean DEBUG_LIFECYCLE = true;

         if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
         if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
         if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
         if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
         if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState +
         if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
         if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
         if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
         if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig);
         if (DEBUG_LIFECYCLE) Slog.v(TAG, "onLowMemory " + this);
         if (DEBUG_LIFECYCLE) Slog.v(TAG, "onTrimMemory " + this + ": " + level);
         if (DEBUG_LIFECYCLE) Slog.v(TAG, "dispatchMultiWindowModeChanged " + this + ": " + isInMultiWindowMode + " " + newConfig);
         if (DEBUG_LIFECYCLE) Slog.v(TAG, "dispatchPictureInPictureModeChanged " + this + ": " + isInPictureInPictureMode + " " + newConfig);

打开ActivityTaskManagerDebugConfig的debug开关,可以查看activity的销毁、调度、释放
?? ?public static boolean DEBUG_ALL_ACTIVITIES = DEBUG_ALL || true;
?? ?public static boolean DEBUG_APP = DEBUG_ALL_ACTIVITIES || false;
? ? public static boolean DEBUG_IDLE = DEBUG_ALL_ACTIVITIES || false;
? ? public static boolean DEBUG_RELEASE = DEBUG_ALL_ACTIVITIES || false;

	//ActivityRecord.java
         if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + this);
         if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + this);
         if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + this);
         if (DEBUG_APP) Slog.v(TAG_APP, "Keeping entry during removeHistory for activity " + this);
	//ActivityTaskSupervisor.java
         if (DEBUG_IDLE) Slog.d(TAG_IDLE, "activityIdleInternal: Callers="  + Debug.getCallers(4));
         if (DEBUG_IDLE) Slogf.i(TAG, "activityIdleInternal(): r=%s, mStartingUsers=%s", r, mStartingUsers);
         if (DEBUG_IDLE) Slog.d(TAG_IDLE, "scheduleIdleTimeout: Callers=" + Debug.getCallers(4));
         if (DEBUG_IDLE) Slog.d(TAG_IDLE, "scheduleIdle: Callers=" + Debug.getCallers(4));
         if (DEBUG_IDLE) Slog.d(TAG_IDLE, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
         if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
         if (DEBUG_IDLE) Slog.d(TAG_IDLE, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
	//WindowProcessController.java
         if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + this);
         if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Abort release; already destroying: " + r);
         if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Not releasing in-use activity: " + r);
         if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + r  + " in state " + r.getState() + " for reason " + reason);

2)添加Log打印

技巧:new Throwable()方法可以打印当前整个方法的调用链

ActivityRecord运行在system_server进程中,ActivityThread运行在App进程中。两处tag可以查看整个Acitivity的变化过程
//ActivityRecord.java
void setState(State state, String reason) {
Slog.e(TAG_STATES, "wei242.wang State movement: " + this + " from:" + getState()+ " to:" + state + " reason:" + reason,new Throwable());
//ActivityThread.java
public void setState(@LifecycleState int newLifecycleState) {
Slog.e(TAG,"wei242.wang setState newLifecycleState = " + newLifecycleState ,new Throwable());

TaskRecord中可查看record的创建及关联(T/U弃用,需要重新研究Task调用关系

?Lcom/android/server/wm/TaskRecord;->topRunningActivityLocked()Lcom/android/server/wm/ActivityRecord;

ActivityStarter.java

//设置ActivityRecord 和TaskRecord的关系
    /** Places {@link #mStartActivity} in {@code task} or an embedded {@link TaskFragment}. */
    private void addOrReparentStartingActivity(@NonNull Task task, String reason) {
        Slog.d(TAG,"wei242.wang addOrReparentStartingActivity",new Throwable());

ActivityStack(T/U弃用,需要重新研究Task调用关系

注:

参考:https://blog.csdn.net/weixin_42695485/article/details/108632021

本文使用U代码

文章来源:https://blog.csdn.net/zinjin_woxin/article/details/135568697
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。