PreferenceFragment :: onPreferenceTreeClick return value - what does it do?

I can't seem to understand how the return value of PreferenceFragment :: onPreferenceTreeClick (...) is interpreted. The docs don't mention how the return value is used (Eclipse says that "@inheritDoc" and the HTML link for Android has an empty body).

I tried to find it in the deprecated API on PreferenceActivity :: onPreferenceTreeClick (...) , but all it says is, well, it's deprecated.

In addition, I tried to return true and false from the method, but it seemed to me that this had no effect on anything.

So - If anyone can be so kind as to tell me that the return value will change?

+6
source share
1 answer

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); } 

return true will return immediately, returning false , will check if an Intent exists for this PreferenceScreen and run the specified Activity .

If you return super.onPreferenceTreeClick(preferenceScreen, preference) , you will also call the following PreferenceFragment code snippet to run

 if (preference.getFragment() != null && getActivity() instanceof OnPreferenceStartFragmentCallback) { return ((OnPreferenceStartFragmentCallback)getActivity()).onPreferenceStartFragment( this, preference); } return false; 

This checks if there is a Fragment to be displayed . If not Preference will look for Intent .

TL; DR

Preferences can start with either Intent or Fragment s. Return value

  • true : nothing happens, fragments and intent are ignored
  • false : fragments are ignored, intentions are executed
  • super.onPreference.. : first tries to fragment the fragment, and the second -

return false; or return super.onPreferenceTreeClick(...) should usually be returned. The meaning of the return value is approximately "Start activity on purpose, if exists?". You must return true if you specified the intention, but do not want to run it. This does not matter in most other cases, as you rarely process clicks if you have this intention.

+11
source

Source: https://habr.com/ru/post/945240/


All Articles