[android-developers] How to create new PreferenceFragment from XML
I want to launch a new preference screen from my main preference screen.
My main preference.xml is this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen
android:title="@string/pref_settings_title" >
<CheckBoxPreference
android:key="settingsMute"
android:title="@string/pref_muteAudio"
android:summary="@string/pref_muteAudio_summ"
android:defaultValue="false" />
<CheckBoxPreference
android:key="settingsObjectFilter"
android:title="@string/pref_enableObjectFilters"
android:summary="@string/pref_enableObjectFilters_summ"
android:defaultValue="false" />
<CheckBoxPreference
android:key="settingsNoStateFilter"
android:title="@string/pref_filterObjectsNoState"
android:summary="@string/pref_filterObjectsNoState_summ"
android:defaultValue="false" />
<CheckBoxPreference
android:key="settingsEnableService"
android:title="@string/pref_enableService"
android:summary="@string/pref_enableService_summ"
android:defaultValue="false" />
</PreferenceScreen>
<PreferenceScreen
android:fragment="com.example.android.misterhouse.activity.SettingsActivity$HostsFragment"
android:title="@string/pref_hosts_title"
android:summary="@string/pref_hosts_title" >
</PreferenceScreen>
<EditTextPreference
android:title="@string/pref_version_title"
android:summary="@string/about_current"
android:selectable="false" />
</PreferenceScreen>
When selecting the "pref_hosts_title", I want the SettingsActivity$HostsFragment to display.
This new fragment does display, however, it doesn't replace the original fragment, it
instead overlays the original fragment. The two fragments display at the same time.
What might I be doing wrong?
The activity which uses these XML are as follows:
public class SettingsActivity extends PreferenceActivity {
public static class SettingsFragment extends PreferenceFragment {
public static final String TAG = "SettingsFragment";
@Override
public void onCreate(Bundle savedInstanceState) {
Log.v(TAG,"onCreate");
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
getPreferenceManager( ).setSharedPreferencesMode(MODE_MULTI_PROCESS);
}
}
public static class HostsFragment extends PreferenceFragment {
public static final String TAG = "HostsFragment";
@Override
public void onCreate(Bundle savedInstanceState) {
Log.v(TAG,"onCreate");
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.hosts);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.v(TAG,"onCreate");
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
//addPreferencesFromResource(R.xml.preferences);
// Display the fragment as the main content.
if (android.os.Build.VERSION.SDK_INT >= 11) {
Log.v(TAG,"Building SettingsFragment");
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
}
Any advice appreciated.
Regards,
Jim
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home