源码参考:?AOSPXRef
现象效果
调试UI显示:Settings应用子界面Activity绝大部分都是SubSetting(通过dumpsys指令查看当前活动);
adb shell dumpsys activity activities | grep -i mCurrent
com.android.settings/com.android.settings.SubSettings
代码实现:
(1)Java类文件
- Settings与SubSettings中基本是空Activity,即Activity没有重写任意7大生命周期方法 (/*empty*/) ?
- Settings类和各子xxxSettingsActivity都继承自SettingsActivity类
- Settings类中定义各子静态xxxSettingsActivity类
- 静态内部类在AndroidManifest.xml文件中通过<meta-data>将相应的Fragment绑定起来。
package com.android.settings;
/**
* Top-level Settings activity
*/
public class Settings extends SettingsActivity {
/*
* Settings subclasses for launching independently.
*/
public static class ApnSettingsActivity extends SettingsActivity { /* empty */ }
}
(2)布局文件
(3)代码实现
public class SettingsActivity extends SettingsBaseActivity
implements IMainSwitchBar, PreferenceManager.OnPreferenceTreeClickListener,
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback,
ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
@Override
protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
//...omit some codes
final FeatureFactory factory = FeatureFactory.getFactory(this);
mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
//通过判断当前是Settings还是SubSettings来确定用什么布局来显示
// This is a "Sub Settings" when:
// - this is a real SubSettings
// - or :settings:show_fragment_as_subsetting is passed to the Intent
final boolean isSubSettings = this instanceof SubSettings ||
intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
// If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
// insets.
// If this is in setup flow, don't apply theme. Because light theme needs to be applied
// in SettingsBaseActivity#onCreate().
if (isSubSettings && !WizardManagerHelper.isAnySetupWizard(getIntent())) {
setTheme(R.style.Theme_SubSettings);
}
//设置布局界面
//(0)android 9-
/*mIsShowingDashboard = className.equals(Settings.class.getName());
setContentView(mIsShowingDashboard ?
R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
getSupportFragmentManager().addOnBackStackChangedListener(this);*/
//(1)android 10+
setContentView(R.layout.settings_main_prefs);
//....
if (savedState != null) {
// We are restarting from a previous saved state; used that to initialize, instead
// of starting fresh.
setTitleFromIntent(intent);
ArrayList<DashboardCategory> categories =
savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
if (categories != null) {
mCategories.clear();
mCategories.addAll(categories);
setTitleFromBackStack();
}
} else {
launchSettingFragment(initialFragmentName, intent);
}
}
}
|