这些Activity中并没有实现任何逻辑,那它是怎么加载到自己应有的布局的呢?
在父类SettingsActivity
的onCreate()
中:
@Override
protected void onCreate(Bundle savedState) {
…
// Should happen before any call to getIntent()
// 第一步
getMetaData();
// 第二步
final Intent intent = getIntent();
if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
}
// Getting Intent properties can only be done after the super.onCreate(…)
final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
…
// 第三步
launchSettingFragment(initialFragmentName, intent);
…
}
跟着上面的三个步骤:
第一步
首先通过getMetaData()
获取该Activity在manifest中配置的fragment, 并赋值给mFragmentClass
public static final String META_DATA_KEY_FRAGMENT_CLASS = “com.android.settings.FRAGMENT_CLASS”;
private void getMetaData() {
try {
ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
PackageManager.GET_META_DATA);
if (ai == null || ai.metaData == null) return;
mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
} catch (NameNotFoundException nnfe) {
// No recovery
Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
}
}
那么manifest中是怎么配置的呢?如下:
由此可知WifiInfoActivity
这个Acitivity对应的fragment是:com.android.settings.wifi.WifiInfo
第二步
通过getIntent()构造包含EXTRA_SHOW_FRAGMENT
的intent
public Intent getIntent() {
Intent superIntent = super.getIntent();
String startingFragment = getStartingFragmentClass(superIntent);
// This is called from super.onCreate, isMultiPane() is not yet reliable
// Do not use onIsHidingHeaders either, which relies itself on this method
if (startingFragment != null