Android 入门宝典 - Activity 工作流程

本文详细探讨了Android中Activity的启动流程,从startActivity的调用开始,涉及ActivityManagerService、ActivityThread、Instrumentation等多个关键组件的作用。重点解析了ActivityManagerNative、ActivityManagerService如何交互以及Activity的创建、初始化过程,包括ContextImpl、Application对象的生成,最终调用到Activity的onCreate和onResume方法。
摘要由CSDN通过智能技术生成

startActivity 最终调用 startActivityForResult
· requestCode 判断:添加标志,以便在 Activity 结束时保证有结果返回

if (requestCode >= 0) {
   
    // If this start is requesting a result, we can avoid making
    // the activity visible until the result is received.  Setting
    // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
    // activity hidden during this time, to avoid flickering.
    // This can only be done when a result is requested because
    // that guarantees we will get information back when the
    // activity is finished, no matter what happens to it.
    mStartedActivity = true;
}

ActivityGroup mParent

· ActivityGroup 最开始用来在一个界面嵌入多个子 Activity,现在被 Fragment 代替

Instrumentation 的 execStartActivity 方法:

try {
   
    intent.migrateExtraStreamToClipData();
    intent.prepareToLeaveProcess(who);
    int result = ActivityManager.getService()
        .startActivity(whoThread, who.getBasePackageName(), intent,
                intent.resolveTypeIfNeeded(who.getContentResolver()),
                token, target != null ? target.mEmbeddedID : null,
                requestCode, 0, null, options);
    checkStartActivityResult(result, intent);
} catch (RemoteException e) {
   
    throw new RuntimeException("Failure from system", e);
}

其中 ActivityManager.getService().startActivity 用以启动 Activity,checkStartActivityResult 用以检查启动 Activity 的结果,里面有各种错误异常的返回:

/** @hide */
public static void checkStartActivityResult(int res, Object intent) {
   
    if (!ActivityManager.isStartResultFatalError(res)) {
   
        return;
    }

    switch (res) {
   
        case ActivityManager.START_INTENT_NOT_RESOLVED:
        case ActivityManager.START_CLASS_NOT_FOUND:
            if (intent instanceof Intent && ((Intent)intent).getComponent() != null)
                throw new ActivityNotFoundException(
                        "Unable to find explicit activity class "
                        + ((Intent)intent).getComponent().toShortString()
                        + "; have you declared this activity in your AndroidManifest.xml?");
            throw new ActivityNotFoundException(
                    "No Activity found to handle " + intent);
        case ActivityManager.START_PERMISSION_DENIED:
            throw new SecurityException("Not allowed to start activity "
                    + intent);
        case ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT:
            throw new AndroidRuntimeException(
                    "FORWARD_RESULT_FLAG used while also requesting a result");
        case ActivityManager.START_NOT_ACTIVITY:
            throw new IllegalArgumentException(
                    "PendingIntent is not an activity");
        case ActivityManager.START_NOT_VOICE_COMPATIBLE:
            throw new SecurityException(
                    "Starting under voice control not allowed for: " + intent);
        case ActivityManager.START_VOICE_NOT_ACTIVE_SESSION:
            throw new IllegalStateException(
                    "Session calling startVoiceActivity does not match active session");
        case ActivityManager.START_VOICE_HIDDEN_SESSION:
            throw new IllegalStateException(
                    "Cannot start voice activity on a hidden session");
        case ActivityManager.START_ASSISTANT_NOT_ACTIVE_SESSION:
            throw new IllegalStateException(
                    "Session calling startAssistantActivity does not match active session");
        case ActivityManager.START_ASSISTANT_HIDDEN_SESSION:
            throw new IllegalStateException(
                    "Cannot start assistant activity on a hidden session");
        case ActivityManager.START_CANCELED:
            throw new AndroidRuntimeException("Activity could not be started for "
                    + intent);
        default:
            throw new AndroidRuntimeException("Unknown error code "
                    + res + " when starting " + intent);
    }
}

ActivityManagerService

ActivityManagerNative.getDefault() 的 startActivity

abstract ActivityManagerNative
/**
 * Retrieve the system's default/global activity manager.
 *
 * @deprecated use ActivityManager.getService instead.
 */
static public IActivityManager getDefault() {
   
    return ActivityManager.getService();
}

ActivityManager.getService 返回 IActivityManager 单例:

/**
 * @hide
 */
public static IActivityManager getService() {
   
 return IActivityManagerSingleton.get();
}

/**
 * @hide
 */
private static final Singleton<IActivityManager> IActivityManagerSingleton = 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值