Update preference example to show switching with fragments.

Change-Id: I2b1db31b5e1a0e83d843ad68b0a177048d2914f4
This commit is contained in:
Dianne Hackborn
2010-08-03 13:07:50 -07:00
parent 6e9821c667
commit 132ab286a1
7 changed files with 165 additions and 18 deletions

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.example.android.apis.app;
package com.example.android.apis.preference;
import com.example.android.apis.R;
@@ -33,18 +33,18 @@ import android.widget.TextView;
*/
public class MyPreference extends Preference {
private int mClickCounter;
// This is the constructor called by the inflater
public MyPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setWidgetLayoutResource(R.layout.preference_widget_mypreference);
setWidgetLayoutResource(R.layout.preference_widget_mypreference);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
// Set our custom views inside the layout
final TextView myTextView = (TextView) view.findViewById(R.id.mypreference_widget);
if (myTextView != null) {
@@ -61,14 +61,14 @@ public class MyPreference extends Preference {
// They don't want the value to be set
return;
}
// Increment counter
mClickCounter = newValue;
// Save to persistent storage (this method will make sure this
// preference should be persistent, along with other useful checks)
persistInt(mClickCounter);
// Data has changed, notify so UI can be refreshed!
notifyChanged();
}
@@ -100,7 +100,7 @@ public class MyPreference extends Preference {
* must save the instance state so it is able to, for example, survive
* orientation changes.
*/
final Parcelable superState = super.onSaveInstanceState();
if (isPersistent()) {
// No need to save instance state since it's persistent
@@ -120,14 +120,14 @@ public class MyPreference extends Preference {
super.onRestoreInstanceState(state);
return;
}
// Restore the instance state
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
mClickCounter = myState.clickCounter;
notifyChanged();
}
/**
* SavedState, a subclass of {@link BaseSavedState}, will store the state
* of MyPreference, a subclass of Preference.
@@ -136,10 +136,10 @@ public class MyPreference extends Preference {
*/
private static class SavedState extends BaseSavedState {
int clickCounter;
public SavedState(Parcel source) {
super(source);
// Restore the click counter
clickCounter = source.readInt();
}
@@ -147,7 +147,7 @@ public class MyPreference extends Preference {
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
// Save the click counter
dest.writeInt(clickCounter);
}
@@ -167,5 +167,5 @@ public class MyPreference extends Preference {
}
};
}
}

View File

@@ -30,21 +30,44 @@ import java.util.List;
*/
//BEGIN_INCLUDE(activity)
public class PreferenceWithHeaders extends PreferenceActivity {
/**
* Populate the activity with the top-level headers.
*/
@Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preference_headers, target);
}
/**
* This fragment shows the preferences for the first header.
*/
public static class Prefs1Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
addPreferencesFromResource(R.xml.fragmented_preferences);
}
}
/**
* This fragment contains a second-level set of preference that you
* can get to by tapping an item in the first preferences fragment.
*/
public static class Prefs1FragmentInner extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.fragmented_preferences_inner);
}
}
/**
* This fragment shows the preferences for the second header.
*/
public static class Prefs2Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {