版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/crazy1235/article/details/78013971
转载请注明出处:http://blog.csdn.net/crazy1235/article/details/78013971
从Android手机屏幕上点击一个应用图标进行启动Activity的过程分析如下:
/packages/apps/Launcher3/src/com/android/launcher3/Launcher.java
public class Launcher extends Activity
Launcher # startActivitySafely()
@Thunk boolean startActivitySafely(View v, Intent intent, Object tag) {
boolean success = false;
if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
return false;
}
// ...关键代码
success = startActivity(v, intent, tag);
} catch (ActivityNotFoundException e) {
// 没有找到Activity
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;
}
Launcher # startActivity()
private boolean startActivity(View v, Intent intent, Object tag) {
// 添加flag
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// ... 省略代码
try{
startActivity(intent, optsBundle);
return true;
} catch(){
}
return false;
}
下面的分析过程可参考 Activity具体是怎么创建的?又是怎么显示出来的?
Activity # startActivity()
@Override
public void startActivity(Intent intent, @Nullable Bundle options) {
if (options != null) {
startActivityForResult(intent, -1, options);
} else {
startActivityForResult(intent, -1);
}
}