First question on stackoverflow, please forgive any inconsistencies.
My Android app implements a PreferenceFragmentCompat fragment which is opened through a button click in the MainActivity. All the options in "root" PreferenceScreen work smoothly but I cannot open any "child" PreferenceScreens.
After a lot of search, I found the need to implement the onPreferenceStartScreen callback in my fragment and it did work! But now, I made quite a few changes to the app and must have screwed something up and can't figure out what.
So here it goes!
Among others, I implement these 2 libraries in my app level gradle.build
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.preference:preference:1.1.1'
This is my test pref3.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:defaultValue="false"
android:key="check_box_preference_1"
android:title="Check box preference" />
<PreferenceScreen android:title="Preference Screen">
<CheckBoxPreference
android:defaultValue="false"
android:key="check_box_preference_2"
android:title="Check box preference" />
</PreferenceScreen>
</PreferenceScreen>
This is my test java Preferences fragment (Common.log is my utility logger method)
public class TestPrefFrag extends PreferenceFragmentCompat implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
private static final String TAG = "TestPrefFrag";
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
Common.log(5, TAG, "onCreatePreferences: started");
setPreferencesFromResource(R.xml.prefs3, rootKey);
}
@Override
public boolean onPreferenceStartScreen(PreferenceFragmentCompat caller, PreferenceScreen preferenceScreen) {
Common.log(5, TAG, "onPreferenceStartScreen: '" + caller.getTag() + "' called for key '" + preferenceScreen.getKey() + "'");
caller.setPreferenceScreen(preferenceScreen);
return true;
}
@Override
public void onNavigateToScreen(PreferenceScreen preferenceScreen) {
Common.log(5, TAG, "onNavigateToScreen: called for key '" + preferenceScreen.getKey() + "'");
//getCallbackFragment();
super.onNavigateToScreen(preferenceScreen);
}
@Override
public boolean onPreferenceTreeClick(Preference preference) {
Common.log(5, TAG, "onPreferenceTreeClick: detected click @ '" + preference.getKey() + "'");
return super.onPreferenceTreeClick(preference);
}
}
When I run this
onPreferenceTreeClick
onlyonNavigateToScreen
and then the onPreferenceTreeClick
but never the onPreferenceStartScreen
Shouldn't onPreferenceStartScreen
be called right after onNavigateToScreen
.
What am I doing wrong?
Thanks for any help!
You need to override the PreferenceFragment
method getCallbackFragment
, like this
@Override
public Fragment getCallbackFragment() {
return this;
}