版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Cute_Code/article/details/79123569
上图为整个Activity的启动流程
接下来我们大概分析
在我们的Android系统中,应用程序是由Launcher这个应用启动起来的。当我们安装好应用程序之后,就会在Launcher的界面上生成一个图标,我们点击图标时Launch就会启动我们的应用程序。
1.点击图标,launcher调用onClick方法
/**
*
*
* @param v The view representing the clicked shortcut. 1.此处的view是指被点击的桌面图标
*/
public void onClick(View v) {
// Make sure that rogue clicks don't get through while allapps is
// launching, or after the
// view has detached (it's possible for this to happen if the view is
// removed mid touch).
if (v.getWindowToken() == null) {
return;
}
if (!mWorkspace.isFinishedSwitchingState()) {
return;
}
Object tag = v.getTag();
if (tag instanceof ShortcutInfo) {
// 打开快捷方式对应的intent
final Intent intent = ((ShortcutInfo) tag).intent;
int[] pos = new int[2];
v.getLocationOnScreen(pos);
intent.setSourceBounds(new Rect(pos[0], pos[1], pos[0]
+ v.getWidth(), pos[1] + v.getHeight()));
boolean success = startActivitySafely(v, intent, tag);//2.同时调用startActivitySafely
if (success && v instanceof BubbleTextView) {
mWaitingForResume = (BubbleTextView) v;
mWaitingForResume.setStayPressed(true);
}
} else if (tag instanceof FolderInfo) {
if (v instanceof FolderIcon) {
// 打开文件夹
FolderIcon fi = (FolderIcon) v;
handleFolderClick(fi);
}
} else if (v == mAllAppsButton) {
// 显示或者不显示“全部程序”界面
if (isAllAppsVisible()) {
showWorkspace(true);
} else {
onClickAllAppsButton(v);
}
}
}
boolean startActivitySafely(View v, Intent intent, Object tag) {
boolean success = false;
try {
success = startActivity(v, intent, tag); 由此可见StartActivitySafely(),最终是调用startActivity()方法
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.activity_not_found,
Toast.LENGTH_SHORT).show();
Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
}
return success;
}
boolean startActivity(View v, Intent intent, Object tag) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
// Only launch using the new animation if the shortcut has not opted
// out (this is a
// private contract between launcher and may be ignored in the
// future).
boolean useLaunchAnimation = (v != null)
&& !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
if (useLaunchAnimation) {
ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v,
0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
startActivity(intent, opts.toBundle());
} else {
startActivity(intent);
}
return true;
} catch (SecurityException e) {
Toast.makeText(this, R.string.activity_not_found,
Toast.LENGTH_SHORT).show();
Log.e(TAG,
"Launcher does not have the permission to launch "
+ intent
+ ". Make sure to create a MAIN intent-filter for the corresponding activity "
+ "or use the exported attribute for this activity. "
+ "tag=" + tag + " intent=" + intent, e);
}
return false;
}
3.在startActivity()方法中,intent 添加了flag FLAG_ACTIVITY_NEW_TASK , 此标志为创建新的任务栈,在创建目标
任务栈之前,首先会调用ActivityThread类启动launcher的进程,然后启动目标应用的任务栈,启动完成之后,目标应用的任务栈会通知
ActivityThread调用launcher的onPause方法。此时,ActivityThread启动新的应用进程(也就是目标应用的进程)。
4.目标应用的进程通过loadClass加载MainActivity,然后通过H(handler)来控制MainActivity的生命周期