AndroidManifest的activity标签中配置的theme属性的生效流程大体如下:
1.android.content.pm.PackageParser.parseActivity()接口中读取AndroidManifest的activity标签中配置的theme属性,代码如下:
a.info.theme = sa.getResourceId(R.styleable.AndroidManifestActivity_theme, 0);
说明:
① a.info为android.content.pm.ActivityInfo对象实例。
2.android.content.pm.ActivityInfo.getThemeResource()接口中获取当前Activity的theme,代码如下:
public final int getThemeResource() {
return theme != 0 ? theme : applicationInfo.theme;
}
说明:
① 如果当前Activity没有在AndroidManifest中配置theme属性的话,则使用Application的默认theme。
3.android.app.ActivityThread.performLaunchActivity()接口中设置theme,然后调用Activity的onCreate接口,代码如下:
int theme = r.activityInfo.getThemeResource();
if (theme != 0) {
activity.setTheme(theme);
}
activity.mCalled = false;
if (r.isPersistable()) {
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
mInstrumentation.callActivityOnCreate(activity, r.state);
}
说明:
① 在AndroidManifest配置的theme,最终也是调用setTheme()接口生效的。
② mInstrumentation.callActivityOnCreate()接口中调用了Activity的onCreate()接口。
③ 从上述逻辑可见,如果需要在Activity代码中调用setTheme()接口的话,推荐在super.onCreate()接口调用之前。