Repository navigation
Add annotation helpers to PreferenceActivity #7
Description
Activity
Are there any Updates to this issue? Prefrence- and PreferenceFragments-support would be really nice.
No. Go ahead if you want to write some spec :)
- added a commit that references this issue
on Apr 13, 2013 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.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.@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.
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?I vote for
Preference.OnPreferenceChangeListenerbecause it has several benefits:- it is not related to the global
SharedPreferences, so we do not have to remove the listener when theActivity/Fragmentis 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,OnSharedPreferenceChangeListeneronly gets called if the preference store itself is modified.
- it is not related to the global
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:
@Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.equals("tracklist_load_releaseimage")) { ListPreference connectionPref = (ListPreference) findPreference(key); if (connectionPref != null) { connectionPref.setSummary(connectionPref.getEntry()); } } else if (key.equals("alarmclock_timeout")) { ListPreference alarmTimeoutPref = (ListPreference) findPreference(key); if (alarmTimeoutPref != null) { alarmTimeoutPref.setSummary(getString(R.string.pref_alarmclock_timeout_summary, alarmTimeoutPref.getEntry())); } } else if (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
SharedPreferencesprogrammatically, becuse in that casePreference.OnPreferenceChangeListeneris not called, onlyOnSharedPreferenceChangeListener.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 ^^)
Forgot to close this. 😀
@pyricau, Jan 5, 2011
It might be a good idea to provide helpers for PreferenceActivity.
Things such as @PreferenceScreen(R.xml.my_preference), and @preference("key") on a field.
Some annotations to had ValueChangeListeners might be nice too.
mail.michael.ludwig, Mar 20, 2011
Yes that would be very nice. Also clickHandlers to Preferences would be nice.
@pyricau, Jul 20, 2011
We should look at what RoboGuice already provides, to integrate the same kind of functionalities.