How to get value for PreferenceFragment

I have this code:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference android:key="notification_settings"
        android:text="@string/impostazione"
        android:id="@+id/save_check"
        android:summary="@string/notification_title"
        android:defaultValue="false"

        ></CheckBoxPreference>

</PreferenceScreen>

      

now I need to get the Checkbox value in FragmentA:

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());

      

and now?

+3


source to share


3 answers


Try this way, hope it helps you solve your problem.

sharedPreferences.getBoolean("notification_settings",false)

      



getBoolean () : it takes two parameters one and the other is the default

+2


source


sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
boolean isChecked = sharedPreferences.getBoolean(getString(R.string.notification_settings), false);

      



0


source


strings.xml
<string name="notification_setting">notification_settings</string>
---------------------------------
xxx.xml
.....
<CheckBoxPreference android:key="@string/notification_setting"
....
--------------------------------
.java
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
boolean wifiState = sharedPreferences.getBoolean(R.string.setting_wifi_key, false);

wifiState is you value.

      

0


source







All Articles