首先推荐一个查看源码的Android studio 插件 AndroidSourceViewer,超级好用。
我是跟着《Android进阶解密》这本书来学习源码的,也推荐给大家。
直接上启动流程第一部分的流程图
Activity的启动一般是从startActivity开始的,
@Override
public void startActivity(Intent intent, @Nullable Bundle options) {
if (options != null) {
startActivityForResult(intent, -1, options);
} else {
// Note we want to go through this call for compatibility with
// applications that may have overridden the method.
startActivityForResult(intent, -1);
}
}
startActivity其实也是调用的startActivityForResult,
1、当不设置FLAG_ACTIVITY_NEW_TASK时,新启动的Activity会被添加到调用者的栈中
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
@Nullable Bundle options) {
if (mParent == null) {
options = transferSpringboardActivityOptions(options);
Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode, options);
if (ar != null) {
mMainThread.sendActivityResult(
mToken, mEmbeddedID, requestCode, ar.getResultCode(),
ar.getResultData());
}
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;
}
cancelInputsAndStartExitTransition(options);
// TODO Consider clearing/flushing other event sources and events for child windows.
} else {
if (options != null) {
mParent.startActivityFromChild(this, intent, requestCode, options);
} else {
// Note we want to go through this method for compatibility with
// existing applications that may have overridden it.
mParent.startActivityFromChild(this, intent, requestCode);
}
}
}
2、如果requestCode小于0,则和调用startActivity的效果一样,不会有返回值
3、如果在startActivityForResult时使用FLAG_ACTIVITY_NEW_TASK,那会启动一个新的栈,并立马收到CANCEL
可以看到这里根据mParent是否为空进行了不同的处理,
问题1:mParent代表什么,什么时候进行赋值?
首先看一下mParent为null的情况,会执行mInstrumentation.execStartActivity方法。
Instrumentation主要用来监控应用程序和系统的交互。需要在AndroidManifest中通过<instrumentation>声明用于监控应用与系统交互的 Instrumentation 类。Instrumentation 对象会在应用的所有组件之前进行实例化。
public ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, String target,
Intent intent, int requestCode, Bundle options) {
IApplicationThread whoThread = (IApplicationThread) contextThread;
if (mActivityMonitors != null) {
synchronized (mSync) {
final int N = mActivityMonitors.size();
for (int i=0; i<N; i++) {
final ActivityMonitor am = mActivityMonitors.get(i);
ActivityResult result = null;
if (am.ignoreMatchingSpecificIntents()) {
result = am.onStartActivity(intent);
}
if (result != null) {
am.mHits++;
return result;
} else if (am.match(who, null, intent)) {
am.mHits++;
if (am.isBlocking()) {
return requestCode >= 0 ? am.getResult() : null;
}
break;
}
}
}
}
try {
intent.migrateExtraStreamToClipData();
intent.prepareToLeaveProcess(who);
int result = ActivityManager.getService()
.startActivity(whoThread, who.getBasePackageName(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, target, requestCode, 0, null, options);
checkStartActivityResult(result, intent);
} catch (RemoteException e) {
throw new RuntimeException("Failure from system", e);
}
return null;
}
mActivityMonitors部分的代码,我的理解应该是给测试功能使用的,所以看下边ActivityManager.getService().startActivity的调用。
ActivityManager是个工具类,应用开发中很少用到,不过isLowRamDevice()和clearApplicationUserData()可以在应用开发中用来满足特殊的需求。
getService中用到了单例类Singleton
public abstract class Singleton<T> {
private T mInstance;
protected abstract T create();
public final T get() {
synchronized (this) {
if (mInstance == null) {
mInstance = create();
}
return mInstance;
}
}
}
持续更新中。。。