Code Monkey home page Code Monkey logo

Comments (11)

keineantwort avatar keineantwort commented on June 8, 2024

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

from androidannotations.

pyricau avatar pyricau commented on June 8, 2024

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

from androidannotations.

WonderCsabo avatar WonderCsabo commented on June 8, 2024

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?

from androidannotations.

dodgex avatar dodgex commented on June 8, 2024

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.

from androidannotations.

WonderCsabo avatar WonderCsabo commented on June 8, 2024
  • @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.

from androidannotations.

dodgex avatar dodgex commented on June 8, 2024

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?

from androidannotations.

WonderCsabo avatar WonderCsabo commented on June 8, 2024

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.

from androidannotations.

dodgex avatar dodgex commented on June 8, 2024

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

from androidannotations.

WonderCsabo avatar WonderCsabo commented on June 8, 2024

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

from androidannotations.

dodgex avatar dodgex commented on June 8, 2024

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

from androidannotations.

WonderCsabo avatar WonderCsabo commented on June 8, 2024

Forgot to close this. ๐Ÿ˜€

from androidannotations.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.