Makarele
Makarele

Reputation: 59

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.

Upvotes: 2

Views: 1547

Answers (1)

daemmie
daemmie

Reputation: 6460

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" />
...

Upvotes: 2

Related Questions