Search code examples
androidpreference

Get preference in mainactivity


I've got a Preferenceactivity with a Preferenceswitch. By default my app launches with fragment 1, how can I do that if Preferenceswitch is fliped/activated, fragment 2 launches at app start?

What I'm looking for is the command to access the preference from the mainactivity.


Solution

  • OK so I guess you have you have got some preference Activity (which uses an xml file) and now you want to get the preferences in an other Activity.

    So for your switch is something like this:

     SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
      // enter the key from your xml and the default value
      boolean value = sharedPreferences.getBoolean("yourkey",false); 
    

    If the xml pref looks like this:

    ....
    <SwitchPreference
       android:key="yourkey"
       android:title="Test"
       android:defaultValue="false" />
    ...