activity token

1 Overview

    The startup process of activity involves many layers in the Android framework, such as activity manager, window manager, and client application APP. How to achieve one-to-one correspondence between them?
write picture description here

2. Token creation:

    The activity startup process will create an ActivityRecord object to record the information of the activity, and when the constructor is executed, it will create a token Token
–>ActivityRecord.java

ActivityRecord(){
   appToken = new Token(this, service);
}

3. The Token is passed to the WMS process:

    When the ActivityRecord is created, it will be used as a parameter to start the activity. This process will pass the token token to WMS (window manager)
-> ActivityStack.java

final void startActivityLocked(ActivityRecord r, boolean newTask,
                               boolean doResume, boolean keepCurTransition, Bundle options) {
......
     mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
        r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
        (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
        r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind);
......
}

    WMS (window manager) will create the object AppWindowToken according to the parameter token, and put the AppWindowToken into the mTokenMap container to record. When you want to addWindow later, you can traverse mTokenMap to see if there is a corresponding token token.
–>WindowManagerService.java

public void addAppToken(int addPos, IApplicationToken token...){
......
atoken = new AppWindowToken(this, token, voiceInteraction);
atoken.inputDispatchingTimeoutNanos = inputDispatchingTimeoutNanos;
atoken.appFullscreen = fullscreen;
atoken.showForAllUsers = showForAllUsers;
atoken.requestedOrientation = requestedOrientation;
atoken.layoutConfigChanges = (configChanges &
        (ActivityInfo.CONFIG_SCREEN_SIZE | ActivityInfo.CONFIG_ORIENTATION)) != 0;
atoken.mLaunchTaskBehind = launchTaskBehind;
if (DEBUG_TOKEN_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG, "addAppToken: " + atoken
        + " to stack=" + stackId + " task=" + taskId + " at " + addPos);

Task task = mTaskIdToTask.get(taskId);
if (task == null) {
    task = createTaskLocked(taskId, stackId, userId, atoken);
}
task.addAppToken(addPos, atoken);

mTokenMap.put(token.asBinder(), atoken);
......
}

3. Token is passed to the application client process:

    When AMS and WMS are ready, they will send messages through scheduleLaunchActivity and call back the client to execute the oncreate method of the activity life cycle. At this point, the activity token r.appToken is passed to the client as a parameter.
–>ActivityStackSupervisor.java

final boolean realStartActivityLocked(.....) {
......
app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
        System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
        new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
        task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
        newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
......
}

    The client-side scheduleLaunchActivity method is implemented as follows:

public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
        ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
        CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
        int procState, Bundle state, PersistableBundle persistentState,
        List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
        boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
    updateProcessState(procState, false);

    ActivityClientRecord r = new ActivityClientRecord();

    r.token = token;
......
    sendMessage(H.LAUNCH_ACTIVITY, r);//发送信息执行oncreate方法
}

4. Summary

    Through the above analysis, it can be clearly seen that the client, AMS, and WMS only have activity tokens. Through the token token, you can ensure that the activities can maintain a one-to-one correspondence at different levels.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324601885&siteId=291194637