Activity的performStart用于执行ActivityStart,代码如下:
//frameworks/base/core/java/android/app/Activity.java
public class Activity extends ContextThemeWrapper
implements LayoutInflater.Factory2,
Window.Callback, KeyEvent.Callback,
OnCreateContextMenuListener, ComponentCallbacks2,
Window.OnWindowDismissedCallback,
ContentCaptureManager.ContentCaptureClient {
final void performStart(String reason) {
dispatchActivityPreStarted();
mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
mFragments.noteStateNotSaved();
mCalled = false;
mFragments.execPendingActions();
mInstrumentation.callActivityOnStart(this);
EventLogTags.writeWmOnStartCalled(mIdent, getComponentName().getClassName(), reason);
if (!mCalled) {
throw new SuperNotCalledException(
"Activity " + mComponent.toShortString() +
" did not call through to super.onStart()");
}
mFragments.dispatchStart();
mFragments.reportLoaderStart();
// Warn app developers if the dynamic linker logged anything during startup.
boolean isAppDebuggable =
(mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
if (isAppDebuggable) {
String dlwarning = getDlWarning();
if (dlwarning != null) {
String appName = getApplicationInfo().loadLabel(getPackageManager())
.toString();
String warning = "Detected problems with app native libraries\n" +
"(please consult log for detail):\n" + dlwarning;
if (isAppDebuggable) {
new AlertDialog.Builder(this).
setTitle(appName).
setMessage(warning).
setPositiveButton(android.R.string.ok, null).
setCancelable(false).
show();
} else {
Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show();
}
}
}
GraphicsEnvironment.getInstance().showAngleInUseDialogBox(this);
mActivityTransitionState.enterReady(this);
dispatchActivityPostStarted();
}
}
上面方法主要处理如下:
1、调用mInstrumentation(Instrumentation)的callActivityOnStart方法。
2、调用dispatchActivityPostStarted方法。
下面分别进行分析:
Instrumentation callActivityOnStart
调用mInstrumentation(Instrumentation)的callActivityOnStart方法:
//frameworks/base/core/java/android/app/Instrumentation.java
public class Instrumentation {
public void callActivityOnStart(Activity activity) {
activity.onStart();
}
}
Activity onStart
调用Activity的onStart方法:
void onStart():在 {@link #onCreate} 之后调用,或者在 {@link #onRestart} 之后调用,当活动已停止时,但现在再次向用户显示。
在其子类(应用程序实现)的onStart会调用super.onStart()方法:
//frameworks/base/core/java/android/app/Activity.java
public class Activity extends ContextThemeWrapper
implements LayoutInflater.Factory2,
Window.Callback, KeyEvent.Callback,
OnCreateContextMenuListener, ComponentCallbacks2,
Window.OnWindowDismissedCallback,
ContentCaptureManager.ContentCaptureClient {
protected void onStart() {
if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStart " + this);
mCalled = true;
mFragments.doLoaderStart();
dispatchActivityStarted();
getAutofillClientController().onActivityStarted();
}
}
Activity dispatchActivityStarted
调用Activity的dispatchActivityStarted方法:
//frameworks/base/core/java/android/app/Activity.java
public class Activity extends ContextThemeWrapper
implements LayoutInflater.Factory2,
Window.Callback, KeyEvent.Callback,
OnCreateContextMenuListener, ComponentCallbacks2,
Window.OnWindowDismissedCallback,
ContentCaptureManager.ContentCaptureClient {
private void dispatchActivityStarted() {
getApplication().dispatchActivityStarted(this);
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i = 0; i < callbacks.length; i++) {
((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityStarted(this);
}
}
}
}
Application.ActivityLifecycleCallbacks onActivityStarted
调用Application.ActivityLifecycleCallbacks的onActivityStarted方法,如果应用程序调用了Application的registerActivityLifecycleCallbacks,就会收到onActivityStarted回调。
void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState):在 Activity 调用 {@link Activity#onCreate super.onCreate()} 时调用。
Activity dispatchActivityPostStarted
调用Activity的dispatchActivityPostStarted方法:
//frameworks/base/core/java/android/app/Activity.java
public class Activity extends ContextThemeWrapper
implements LayoutInflater.Factory2,
Window.Callback, KeyEvent.Callback,
OnCreateContextMenuListener, ComponentCallbacks2,
Window.OnWindowDismissedCallback,
ContentCaptureManager.ContentCaptureClient {
private void dispatchActivityPostStarted() {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i = 0; i < callbacks.length; i++) {
((Application.ActivityLifecycleCallbacks) callbacks[i])
.onActivityPostStarted(this);
}
}
getApplication().dispatchActivityPostStarted(this);
}
}
Application.ActivityLifecycleCallbacks onActivityPostStarted
调用Application.ActivityLifecycleCallbacks的onActivityPostStarted方法,如果应用程序调用了Application的registerActivityLifecycleCallbacks,就会收到onActivityPostStarted回调。
void onActivityPostStarted(@NonNull Activity activity):作为正在启动的活动的最后一步调用。这始终在 {@link Activity#onStart} 之后调用。