Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Add annotation helpers to PreferenceActivity #7

Description

@pyricau

@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.

Activity

  1. keineantwort commented on Jan 31, 2013

    @keineantwort

    Are there any Updates to this issue? Prefrence- and PreferenceFragments-support would be really nice.

  2. pyricau commented on Feb 27, 2013

    @pyricau
    ContributorAuthor

    No. Go ahead if you want to write some spec :)

  3. added a commit that references this issue on Apr 13, 2013
    d561e23
  4. WonderCsabo commented on Sep 23, 2014

    @WonderCsabo
    Member

    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.

    @yDelouis, @DayS, @dodgex WDYT?

  5. dodgex commented on Sep 23, 2014

    @dodgex
    Member

    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)

    not sure what you mean with @Preference.

    this should support PreferenceActivity, PreferenceFragment and thier corresporending support-v4 versions if they exist.

  6. WonderCsabo commented on Sep 23, 2014

    @WonderCsabo
    Member
    • @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.
  7. dodgex commented on Sep 23, 2014

    @dodgex
    Member

    okay. sure. got it. sorry ;)

    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?

  8. WonderCsabo commented on Sep 23, 2014

    @WonderCsabo
    Member

    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.
  9. dodgex commented on Sep 23, 2014

    @dodgex
    Member

    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:

    @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());
        }
    }
  10. WonderCsabo commented on Sep 23, 2014

    @WonderCsabo
    Member

    Yes, but only if you do not modify the SharedPreferences programmatically, becuse in that case Preference.OnPreferenceChangeListener is not called, only OnSharedPreferenceChangeListener.

  11. dodgex commented on Sep 23, 2014

    @dodgex
    Member

    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 ^^)

  12. WonderCsabo commented on Jun 5, 2015

    @WonderCsabo
    Member

    Forgot to close this. 😀

  13. removed this from the Someday milestone on Jun 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions