Example usage for android.preference MultiSelectListPreference setKey

List of usage examples for android.preference MultiSelectListPreference setKey

Introduction

In this page you can find the example usage for android.preference MultiSelectListPreference setKey.

Prototype

public void setKey(String key) 

Source Link

Document

Sets the key for this Preference, which is used as a key to the SharedPreferences or PreferenceDataStore .

Usage

From source file:org.xbmc.kore.ui.SettingsFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);

    // Get the preference for side menu itens and change its Id to include
    // the current host
    MultiSelectListPreference sideMenuItens = (MultiSelectListPreference) findPreference(
            Settings.KEY_PREF_NAV_DRAWER_ITEMS);
    hostId = HostManager.getInstance(getActivity()).getHostInfo().getId();
    sideMenuItens.setKey(Settings.getNavDrawerItemsPrefKey(hostId));

    // HACK: After changing the key dinamically like above, we need to force the preference
    // to read its value. This can be done by calling onSetInitialValue, which is protected,
    // so, instead of subclassing MultiSelectListPreference and make it public, this little
    // hack changes its access mode.
    // Furthermore, only do this is nothing is saved yet on the shared preferences,
    // otherwise the defaults won't be applied
    if (getPreferenceManager().getSharedPreferences().getStringSet(Settings.getNavDrawerItemsPrefKey(hostId),
            null) != null) {/*from  ww w . jav  a 2 s .co  m*/
        Class iterClass = sideMenuItens.getClass();
        try {
            Method m = iterClass.getDeclaredMethod("onSetInitialValue", boolean.class, Object.class);
            m.setAccessible(true);
            m.invoke(sideMenuItens, true, null);
        } catch (Exception e) {
        }
    }

    // Check permission for phone state and set preference accordingly
    if (!hasPhonePermission()) {
        TwoStatePreference pauseCallPreference = (TwoStatePreference) findPreference(
                Settings.KEY_PREF_PAUSE_DURING_CALLS);
        pauseCallPreference.setChecked(false);
    }

    setupPreferences();
}