Move switch preference into its own demo.

This avoids everything breaking on pre-ICS platforms.

Also get rid of pref initialization in Application. I hate that.  Hate
hate hate.  Hate.  Totally hate.

Totally.

Change-Id: Idb3526a96eb2dff49f9de8e5ae71149cb4ed6e96
This commit is contained in:
Dianne Hackborn
2011-11-14 14:28:42 -08:00
parent d673b70dfd
commit 2fd75c6368
11 changed files with 123 additions and 50 deletions

View File

@@ -16,9 +16,9 @@
package com.example.android.apis.preference;
import com.example.android.apis.ApiDemosApplication;
import com.example.android.apis.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
@@ -34,20 +34,28 @@ import android.preference.PreferenceManager;
* {@link PreferenceManager#setDefaultValues(android.content.Context, int, boolean)}.
* <p>
* This should be called early, typically when the application is first created.
* This ensures any of the application's activities, services, etc. will have
* the default values present, even if the user has not wandered into the
* application's settings. For ApiDemos, this is {@link ApiDemosApplication},
* and you can find the call to
* {@link PreferenceManager#setDefaultValues(android.content.Context, int, boolean)}
* in its {@link ApiDemosApplication#onCreate() onCreate}.
* An easy way to do this is to have a common function for retrieving the
* SharedPreferences that takes care of calling it.
*/
public class DefaultValues extends PreferenceActivity {
// This is the global (to the .apk) name under which we store these
// preferences. We want this to be unique from other preferences so that
// we do not have unexpected name conflicts, and the framework can correctly
// determine whether these preferences' defaults have already been written.
static final String PREFS_NAME = "defaults";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPrefs(this);
getPreferenceManager().setSharedPreferencesName(PREFS_NAME);
addPreferencesFromResource(R.xml.default_values);
}
static SharedPreferences getPrefs(Context context) {
PreferenceManager.setDefaultValues(context, PREFS_NAME, MODE_PRIVATE,
R.xml.default_values, false);
return context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
}
}

View File

@@ -44,12 +44,9 @@ public class LaunchingPreferences extends Activity implements OnClickListener {
super.onCreate(savedInstanceState);
/*
* If this were my app's main activity, I would load the default values
* so they're set even if the user does not go into the preferences
* screen. Another good place to call this method would be from a
* subclass of Application, so your default values would be loaded
* regardless of entry into your application (for example, a service or
* activity).
* These preferences have defaults, so before using them go apply those
* defaults. This will only execute once -- when the defaults are applied
* a boolean preference is set so they will not be applied again.
*/
PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);

View File

@@ -21,6 +21,7 @@ import com.example.android.apis.R;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Button;
@@ -60,6 +61,12 @@ public class PreferenceWithHeaders extends PreferenceActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Make sure default values are applied. In a real app, you would
// want this in a shared function that is used to retrieve the
// SharedPreferences wherever they are needed.
PreferenceManager.setDefaultValues(getActivity(),
R.xml.advanced_preferences, false);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.fragmented_preferences);
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.preference;
import com.example.android.apis.R;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
public class SwitchPreference extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(this, "switch", MODE_PRIVATE,
R.xml.default_values, false);
// Load the preferences from an XML resource
getPreferenceManager().setSharedPreferencesName("switch");
addPreferencesFromResource(R.xml.preference_switch);
}
}