Settings 启动流程
作者:互联网
Settings 启动流程
-
Settings-->Manifest.xml-->action.MAIN-->SettingsHomepageActivity
public class SettingsHomepageActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Settings首页布局
setContentView(R.layout.settings_homepage_container);
//一级菜单主要内容,跳转至TopLevelSettings显示
showFragment(new TopLevelSettings(), R.id.main_content);
}
}
public class TopLevelSettings extends DashboardFragment {
//根据Fragment生命周期运行
@Override
public void onAttach(Context context) {
super.onAttach(context);
//获取item controler并设置对应的Activity,locate in DashboardFragment.java
use(SupportPreferenceController.class).setActivity(getActivity());
...
//fragment剩余生命周期在执行父类DashboardFragment中的生命周期
}
@Override
protected String getLogTag() {
return TAG;//TAG = "TopLevelSettings"
}
//获取main_item 布局id
@Override
protected int getPreferenceScreenResId() {
return R.xml.top_level_settings;
}
}public abstract class DashboardFragment extends SettingsPreferenceFragment {
...
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Set ComparisonCallback so we get better animation when list changes.
getPreferenceManager().setPreferenceComparisonCallback(
new PreferenceManager.SimplePreferenceComparisonCallback());
if (icicle != null) {
// Upon rotation configuration change we need to update preference states before any
// editing dialog is recreated (that would happen before onResume is called).
//首次不调用,初始化Controller。第二次调用,更新Preference
updatePreferenceStates();
}
}
//Log显示调用方法创建Preference列表
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
refreshAllPreferences(getLogTag());
}
//TopLevelSettings.java重写该方法
protected abstract String getLogTag();
private void refreshAllPreferences(final String TAG) {
final PreferenceScreen screen = getPreferenceScreen();
// First remove old preferences.
if (screen != null) {
// Intentionally do not cache PreferenceScreen because it will be recreated later.
screen.removeAll();
}
// Add resource based tiles.
displayResourceTiles();//静态方式加载Settings一级菜单main item
refreshDashboardTiles(TAG);//动态刷新main_content 状态
final Activity activity = getActivity();
if (activity != null) {
Log.d(TAG, "All preferences added, reporting fully drawn");
activity.reportFullyDrawn();
}
updatePreferenceVisibility(mPreferenceControllers);
}
private void displayResourceTiles() {
final int resId = getPreferenceScreenResId();//TopLevelSettings.java重写该方法获取main_content对应布局id
if (resId <= 0) {
return;
}
addPreferencesFromResource(resId);
}
void refreshDashboardTiles(final String TAG) {
final PreferenceScreen screen = getPreferenceScreen();
final DashboardCategory category =
mDashboardFeatureProvider.getTilesForCategory(getCategoryKey());
if (category == null) {
Log.d(TAG, "NO dashboard tiles for " + TAG);
return;
}
....
}
protected <T extends AbstractPreferenceController> T use(Class<T> clazz) {
List<AbstractPreferenceController> controllerList = mPreferenceControllers.get(clazz);
if (controllerList != null) {
if (controllerList.size() > 1) {
Log.w(TAG, "Multiple controllers of Class " + clazz.getSimpleName()
+ " found, returning first one.");
}
return (T) controllerList.get(0);
}
return null;
}
}
//top_level_settings.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:key="top_level_settings">
<Preference
android:key="top_level_network"
android:title="@string/network_dashboard_title"
android:summary="@string/summary_placeholder"
android:icon="@drawable/ic_homepage_network"
android:order="-120"
android:fragment="com.android.settings.network.NetworkDashboardFragment"
settings:controller="com.android.settings.network.TopLevelNetworkEntryPreferenceController"/>
...
<Preference
android:key="top_level_security" //每个item必须有key属性
android:title="@string/security_settings_title"
android:summary="@string/summary_placeholder"
android:icon="@drawable/ic_homepage_security"
android:order="-40" //负值:绝对值越大越靠前;正值,绝对值越大越靠后
android:fragment="com.android.settings.security.SecuritySettings" //点击item对应跳转的fragment
settings:controller="com.android.settings.security.TopLevelSecurityEntryPreferenceController"/> //当前item的controller;每个item都必须要有该属性,也可直接在相关fragment中实现createPreferenceControllers()方法去调用构造相关controller.
....
</PreferenceScreen>
标签:启动,流程,settings,final,item,TAG,android,void,Settings 来源: https://www.cnblogs.com/a-n-yan/p/16659361.html