1. res\xml\prefs.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <SwitchPreference android:defaultValue="false" android:key="lockApp" android:summary="@string/setting_fragment_lock_app_description" android:title="@string/setting_fragment_lock_app"> </SwitchPreference> </PreferenceScreen>
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.prefs); SwitchPreference lockAppSetting = (SwitchPreference) findPreference("lockApp"); lockAppSetting.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { Boolean isLock = Boolean.parseBoolean(String.valueOf(newValue)); Bundle arguments = new Bundle(); arguments.putBoolean("isLock", isLock); Fragment fragment = new LockPatternFragment(); fragment.setArguments(arguments); mMainActivity.pushFragment(fragment); return true; } }); }