The question is how to build PreferenceFragmentCompat with other nested PreferenceFragmentCompat.
MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.content, new PrefsFragment())
.commit();
}
PrefsFragment:
public class PrefsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
public static class GeneralPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.pref_general, rootKey);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
...
preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Preference
app:fragment="com.example.playground.PrefsFragment$GeneralPreferenceFragment"
android:icon="@drawable/ic_info_black_24dp"
app:title="@string/pref_header_general"
app:key="general" />
<Preference
app:fragment="com.example.playground.PrefsFragment$NotificationPreferenceFragment"
android:icon="@drawable/ic_notifications_black_24dp"
app:title="@string/pref_header_notifications"
app:key="notifications" />
<Preference
app:fragment="com.example.playground.PrefsFragment$DataSyncPreferenceFragment"
android:icon="@drawable/ic_sync_black_24dp"
app:title="@string/pref_header_data_sync"
app:key="data_sync" />
</PreferenceScreen>
So when I click on any preference nothing happens, while I expect my other PreferenceFragment to be loaded. I've read https://developer.android.com/guide/topics/ui/settings/organize-your-settings, which states: When a user taps a Preference with an associated Fragment, the interface method PreferenceFragmentCompat.OnPreferenceStartFragmentCallback.onPreferenceStartFragment() is called. This method is where you should handle displaying the new screen and should be implemented in the surrounding Activity.
But I would like to implement all logic of handling all underlying preferences fragments inside my PrefsFragment, not in parent Activity. Is it possible ?
Thanks for your time and attention.
If you want to split your hierarchy into multiple screens, you can take a look at this code taken directly from Google samples:
You first define the root PreferenceScreen
this way:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
<Preference
app:title="@string/basic_preferences"
app:summary="Sample preferences using basic attributes"
app:fragment="com.example.androidx.preference.sample.MainActivity$BasicPreferencesFragment"/>
<Preference
app:title="@string/widgets"
app:summary="Sample preferences with different widgets"
app:fragment="com.example.androidx.preference.sample.MainActivity$WidgetPreferencesFragment"/>
<Preference
app:title="@string/dialogs"
app:summary="Sample preferences that launch dialogs"
app:fragment="com.example.androidx.preference.sample.MainActivity$DialogPreferencesFragment"/>
<Preference
app:title="@string/advanced_attributes"
app:summary="Sample preferences with advanced attributes"
app:fragment="com.example.androidx.preference.sample.MainActivity$AdvancedPreferencesFragment"/>
</PreferenceScreen>
Then you would have some child PreferenceScreen
s, like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
app:title="@string/basic_preferences">
<SwitchPreferenceCompat
app:key="switch"
app:title="@string/title_switch_preference"
app:summary="@string/summary_switch_preference"/>
<EditTextPreference
app:key="edittext"
app:title="@string/title_edittext_preference"
app:useSimpleSummaryProvider="true"
app:dialogTitle="@string/dialog_title_edittext_preference"/>
<ListPreference
app:key="list"
app:title="@string/title_list_preference"
app:useSimpleSummaryProvider="true"
app:entries="@array/entries"
app:entryValues="@array/entry_values"
app:dialogTitle="@string/dialog_title_list_preference"/>
</PreferenceCategory>
</androidx.preference.PreferenceScreen>
And finally, for example, an Activity
extending AppCompatActivity
with some inner Fragment
s extending PreferenceFragmentCompat
:
package com.example.androidx.preference.sample
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
class MainActivity : AppCompatActivity(),
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, SettingsFragment())
.commit()
supportFragmentManager.addOnBackStackChangedListener {
if (supportFragmentManager.backStackEntryCount == 0) {
setTitle(R.string.title)
}
}
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
override fun onSupportNavigateUp(): Boolean {
if (supportFragmentManager.popBackStackImmediate()) {
return true
}
return super.onSupportNavigateUp()
}
override fun onPreferenceStartFragment(
caller: PreferenceFragmentCompat,
pref: Preference
): Boolean {
// Instantiate the new Fragment
val args = pref.extras
val fragment = supportFragmentManager.fragmentFactory.instantiate(
classLoader,
pref.fragment,
args
).apply {
arguments = args
setTargetFragment(caller, 0)
}
// Replace the existing Fragment with the new Fragment
supportFragmentManager.beginTransaction()
.replace(R.id.settings, fragment)
.addToBackStack(null)
.commit()
return true
}
/**
* The root preference fragment that displays preferences that link to the other preference
* fragments below.
*/
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root, rootKey)
}
}
/**
* A preference fragment that demonstrates commonly used preference attributes.
*/
class BasicPreferencesFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.basic_preferences, rootKey)
}
}
/**
* A preference fragment that demonstrates preferences which contain dynamic widgets.
*/
class WidgetPreferencesFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.widgets, rootKey)
}
}
/**
* A preference fragment that demonstrates preferences that launch a dialog when tapped.
*/
class DialogPreferencesFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.dialogs, rootKey)
}
}
/**
* A preference fragment that demonstrates more advanced attributes and functionality.
*/
class AdvancedPreferencesFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.advanced, rootKey)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With