You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
I need this feature, so i want to implement this ASAP.
This is what i thought:
@PreferenceScreen calls addPreferencesFromResource in init body @Preference ovverides addPreferencesFromResource, and inject the prefs there @AfterPreferences ovverides addPreferencesFromResource, and calls the method
The preference change listener would do the same as the @Preference annotation.
uhm, i think you hav a copy/paste bug in your comment. our shoud @Preference and @AfterPReferences override addPreferencesFromResource? :D
@PreferenceScreen(R.xml.pref) calls addPreferencesFromResource(R.xml.pref) in init body
@AfterPreferences("prefName") implement OnSharedPreferenceChangeListener and override onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) to call the annotated method if key == "prefName" (also call un-/registerOnSharedPreferenceChangeListener(this) in Resume/Pause or whereever)
@Preference (or better @PreferenceByKey) is an annotation to inject a Preference object. So yes, it should override addPreferencesFromResource, so we inject the pref object after the screen has been added.
@AfterPreferences provides a callback for the user to setup the injected Preference objects, like @AfterViews
@PreferenceChange should be the annotation for adding a listener, and not OnSharedPreferenceChangeListener, but Preference.OnPreferenceChangeListener listener.
about @PreferenceChange i can't say much. in my projecst PreferenceFragment i use the SharedPreferences version of the ChangeListener. so maybe listener for both cases could be usefull? eg a shared = true flag?
I vote for Preference.OnPreferenceChangeListener because it has several benefits:
it is not related to the global SharedPreferences, so we do not have to remove the listener when the Activity / Fragment is destroyed, we do not have memory leaks.
It gives the user more control, because it gets called before the value is persisted, the programmer even can change the value.
It works with non-persistent Preferences, OnSharedPreferenceChangeListener only gets called if the preference store itself is modified.
hm. okay. i'm not very familiar with the preferences stuff.
so i could replace the code that currently uses the shared preferences in my PreferenceFragment with the new code that would get generated by AA and stuff still would work? :)
btw this is what i'm doing in my change listener:
@OverridepublicvoidonSharedPreferenceChanged(SharedPreferencessharedPreferences, Stringkey) {
if (key.equals("tracklist_load_releaseimage")) {
ListPreferenceconnectionPref = (ListPreference) findPreference(key);
if (connectionPref != null) {
connectionPref.setSummary(connectionPref.getEntry());
}
} elseif (key.equals("alarmclock_timeout")) {
ListPreferencealarmTimeoutPref = (ListPreference) findPreference(key);
if (alarmTimeoutPref != null) {
alarmTimeoutPref.setSummary(getString(R.string.pref_alarmclock_timeout_summary, alarmTimeoutPref.getEntry()));
}
} elseif (key.equals("analytics_optout")) {
// pref is from @SharedPref(value = Scope.APPLICATION_DEFAULT)GoogleAnalytics.getInstance(getActivity()).setAppOptOut(pref.analytics_optout().get());
}
}
Yes, but only if you do not modify the SharedPreferences programmatically, becuse in that case Preference.OnPreferenceChangeListener is not called, only OnSharedPreferenceChangeListener.
in my PreferencesFragment i do not modify the preferences at all in my code, i only need the change listener to update the summaries or enable/disable google analytics.
anyway: you got my ok for your suggestion. 👍 (not that it would count ^^)
Activity
keineantwort commentedon Jan 31, 2013
Are there any Updates to this issue? Prefrence- and PreferenceFragments-support would be really nice.
pyricau commentedon Feb 27, 2013
No. Go ahead if you want to write some spec :)
Tweak to how click-paging is handled. We now only rely on the scrolle…
WonderCsabo commentedon Sep 23, 2014
I need this feature, so i want to implement this ASAP.
This is what i thought:
@PreferenceScreencallsaddPreferencesFromResourcein init body@PreferenceovveridesaddPreferencesFromResource, and inject the prefs there@AfterPreferencesovveridesaddPreferencesFromResource, and calls the methodThe preference change listener would do the same as the
@Preferenceannotation.@yDelouis, @DayS, @dodgex WDYT?
dodgex commentedon Sep 23, 2014
uhm, i think you hav a copy/paste bug in your comment. our shoud
@Preferenceand@AfterPReferencesoverrideaddPreferencesFromResource? :D@PreferenceScreen(R.xml.pref)callsaddPreferencesFromResource(R.xml.pref)in init body@AfterPreferences("prefName")implementOnSharedPreferenceChangeListenerand overrideonSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)to call the annotated method ifkey == "prefName"(also callun-/registerOnSharedPreferenceChangeListener(this)in Resume/Pause or whereever)not sure what you mean with
@Preference.this should support
PreferenceActivity,PreferenceFragmentand thier corresporending support-v4 versions if they exist.WonderCsabo commentedon Sep 23, 2014
@Preference(or better@PreferenceByKey) is an annotation to inject aPreferenceobject. So yes, it should overrideaddPreferencesFromResource, so we inject the pref object after the screen has been added.@AfterPreferencesprovides a callback for the user to setup the injectedPreferenceobjects, like@AfterViews@PreferenceChangeshould be the annotation for adding a listener, and notOnSharedPreferenceChangeListener, butPreference.OnPreferenceChangeListenerlistener.dodgex commentedon Sep 23, 2014
okay. sure. got it. sorry ;)
about
@PreferenceChangei can't say much. in my projecstPreferenceFragmenti use the SharedPreferences version of the ChangeListener. so maybe listener for both cases could be usefull? eg ashared = trueflag?WonderCsabo commentedon Sep 23, 2014
I vote for
Preference.OnPreferenceChangeListenerbecause it has several benefits:SharedPreferences, so we do not have to remove the listener when theActivity/Fragmentis destroyed, we do not have memory leaks.Preferences,OnSharedPreferenceChangeListeneronly gets called if the preference store itself is modified.dodgex commentedon Sep 23, 2014
hm. okay. i'm not very familiar with the preferences stuff.
so i could replace the code that currently uses the shared preferences in my
PreferenceFragmentwith the new code that would get generated by AA and stuff still would work? :)btw this is what i'm doing in my change listener:
WonderCsabo commentedon Sep 23, 2014
Yes, but only if you do not modify the
SharedPreferencesprogrammatically, becuse in that casePreference.OnPreferenceChangeListeneris not called, onlyOnSharedPreferenceChangeListener.dodgex commentedon Sep 23, 2014
in my PreferencesFragment i do not modify the preferences at all in my code, i only need the change listener to update the summaries or enable/disable google analytics.
anyway: you got my ok for your suggestion. 👍 (not that it would count ^^)
WonderCsabo commentedon Jun 5, 2015
Forgot to close this. 😀