It seems I can't find out how the return value of PreferenceFragment::onPreferenceTreeClick(...) is interpreted. In the docs there's not mentioned how the return value is used (Eclipse says "@inheritDoc" and the Android HTML reference has an empty body).
I tried looking it up in the deprecated API on PreferenceActivity::onPreferenceTreeClick(...) but all it says is, well, it is deprecated.
Additionally I tried returning true and false from the method, but it seemed to me as it had no effect on anything.
So - If anyone could please be so kind to tell me what the return value changes?
In Android apps, there are often settings pages that contain different options the user can tweak. The PreferenceFragment and PreferenceFragmentCompat contains a hierarchy of preference objects displayed on screen in a list. These preferences will automatically save to SharedPreferences as the user interacts with them.
This settings screen is used to manage the preferences of the users. For creating this settings screen android provides a feature to make a settings preferences screen.
findPreference("yourpref"). setEnabled(false);
The code that calls this is in Preference#performClick(PreferenceScreen preferenceScreen)
and it does the following:
PreferenceManager preferenceManager = getPreferenceManager();
if (preferenceManager != null) {
PreferenceManager.OnPreferenceTreeClickListener listener = preferenceManager
.getOnPreferenceTreeClickListener();
if (preferenceScreen != null && listener != null
&& listener.onPreferenceTreeClick(preferenceScreen, this)) {
return;
}
}
if (mIntent != null) {
Context context = getContext();
context.startActivity(mIntent);
}
returning true
will return immediately while returning false
will check if there is an Intent
set for this PreferenceScreen
and start the specified Activity
.
If you return super.onPreferenceTreeClick(preferenceScreen, preference)
you will also cause the following piece of code from PreferenceFragment
to run
if (preference.getFragment() != null &&
getActivity() instanceof OnPreferenceStartFragmentCallback) {
return ((OnPreferenceStartFragmentCallback)getActivity()).onPreferenceStartFragment(
this, preference);
}
return false;
This one checks if there is a Fragment
to be shown. If not Preference
will then look for an Intent
.
Preferences can start either Intent
s or Fragment
s. The meaning of the return value is
true
: nothing happens, both fragments and intents are ignoredfalse
: fragments are ignored, intents are executedsuper.onPreference..
: tries fragment first, intent secondreturn false;
or return super.onPreferenceTreeClick(...)
should be usually the right thing to return. The meaning of the return value is roughly "Start Activity by intent if exist?". You should return true
if you have specified an intent but don't want to start the activity. It does not matter in most other cases since you rarely handle clicks if you have that intent specified.
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