Search code examples
androidsettingssharedpreferencescheckboxpreference

Best way to update checkbox prefefences


I was coding a clock for android. For it, i set a function which updates screen 1 time each a second, so my program can consume a lot of resources, and my objective is to add a checkbox preference, consuming the less resources as I can.

Then, my question is: I have seen some ways to update preferences with onSharedPreferenceChangeListener for example... Which is the way which consumes less system resources? How should I implement it to my code?


Solution

  • if you are using a PreferenceActivity you can implement the OnPreferenceClickListener set a listener on the checkbox then in your onPreferenceClick method with the key you set for the box and do what you need to do with it

    @Override
    public boolean onPreferenceClick(Preference preference) {
        if (preference.getKey().equals("schedulestart")) {
            showDialog(0);
        } else if (preference.getKey().equals("schedulestop")) {
            showDialog(1);
        } else if (preference.getKey().equals("priority")) {
            // Reset unread count when switching mailboxes. They might differ.
            getPreferenceManager().getSharedPreferences().edit().putInt("unreadcount", 0).commit();
        }
        return true;
    }