10

How can I save the the settings of my app? Right now, for example, I have a togglebutton to turn on/off. But if I restart my phone, the toggle button is turned back on. Its not saving the settings if I completely close the app. Can I like the save the settings to the phone as cookies?

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
user2779837
  • 301
  • 3
  • 15

6 Answers6

17

Use Shared Preferences. Like so:

Put this at the top of your class: public static final String myPref = "preferenceName";

Create these methods for use, or just use the content inside of the methods whenever you want:

public String getPreferenceValue()
{
   SharedPreferences sp = getSharedPreferences(myPref,0);
   String str = sp.getString("myStore","TheDefaultValueIfNoValueFoundOfThisKey");
   return str;
}

public void writeToPreference(String thePreference)
{
   SharedPreferences.Editor editor = getSharedPreferences(myPref,0).edit();
   editor.putString("myStore", thePreference);
   editor.commit();
}

You could call them like this:

writeToPreference("on"); // stores that the preference is "on"
writeToPreference("off"); // stores that the preference is "off"

if (getPreferenceValue().equals("on"))
{
   // turn the toggle button on
}
else if (getPreferenceValue().equals("off"))
{
   // turn the toggle button off
}
else if (getPreferenceValue().equals("TheDefaultValueIfNoValueFoundOfThisKey"))
{
   // a preference has not been created
}

Note: you can do this with boolean, integer, etc.

All you have to do is change the String storing and reading to boolean, or whatever type you want.

Here is a link to a pastie with the code above modified to store a boolean instead: http://pastie.org/8400737

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • 1
    Thanks! this is very helpful. I put the calling sharedPereferences in a method. Is there a way to try and retrieving sharedPreferences from another class? Like I want to access the shared pereferences from a broadCastReceiver – user2779837 Oct 14 '13 at 07:09
  • The `shared preferences`, with the way I've written it, can be accessed in any class. Make sure to just keep the `"preferenceName"` which is `myPref` the same in both classes; and keep the `"myStore"` the same as well. – Michael Yaworski Oct 14 '13 at 07:12
  • can you tell me what the myPref is? What isi t used for? – user2779837 Oct 14 '13 at 07:18
  • If I were to access this in another class, do I need to create a SharedPreferences object there as well? – user2779837 Oct 14 '13 at 07:19
  • I also did this: SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); How does that differ from yours? – user2779837 Oct 14 '13 at 07:22
  • You can copy and paste the methods into another class, and call them the exact same way in both. So yes, you need to create the object again, if that's what you mean. `myPref` is just a `String`. You could delete `myPref` and just replace it with `"preferenceName"` in the methods. The `String` just determines which **Preference** you've created is accessed. – Michael Yaworski Oct 14 '13 at 07:25
  • 1
    `SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);` could possibly limit your accessibility to the **Shared Preferences**. To be honest, I don't know. The first time I used **Shared Prefences** (different way that my answer above), it only worked for the class I created it in, so I switched to the way I wrote it in my answer; it always works. Google the difference if you really want to know (Android has documentation). – Michael Yaworski Oct 14 '13 at 07:27
  • Sorry! So in my Activity class I did this: SharedPreferences sp = getSharedPreferences("preferenceName",0); and it works. Now, if I understand you right, if I want to access the SharedPreferences named "preferenceName", I create another SharedPreferences object in the second class? The second class is a BroadCastReceiver and doesnt have getSharedPreferences(), how do I solve this? – user2779837 Oct 14 '13 at 07:35
  • I don't know anything about BroadCastReceiver, but if you can't use the `getSharedPreferences("preferenceName",0)` method, I suggest you Google how to use **Shared Preferences** in BroadCastReceiver. I'm not sure if that's a special class you're talking about or something, but that's all I can say on it. Sorry. Check this out: http://stackoverflow.com/questions/10098981/sharedpreferences-in-broadcastreceiver-seems-to-not-update – Michael Yaworski Oct 14 '13 at 07:51
  • @user2779837 Check these links out: http://stackoverflow.com/questions/10098981/sharedpreferences-in-broadcastreceiver-seems-to-not-update and this http://stackoverflow.com/questions/9075030/shared-preferences-inside-broadcastreceiver – Michael Yaworski Oct 14 '13 at 07:58
2

You need to use SharedPreferences to save the settings of your app locally. Refer this link for more details : http://developer.android.com/reference/android/content/SharedPreferences.html

arshu
  • 11,805
  • 3
  • 23
  • 21
2

Use SharedPreferences as,

To Save:

 SharedPreferences settings;
 SharedPreferences.Editor editor;
 public static final String PREFS_NAME = "app_pref";
 public static final String KEY_p_id = "KEY_test";

    settings = getSharedPreferences(PREFS_NAME, 0);
    editor = settings.edit();
    editor.putString(Login_screen.KEY_test, values.get(0));
    editor.commit();

To Remove:

    editor.remove("KEY_test").commit();
Mitesh Shah
  • 1,729
  • 2
  • 19
  • 37
0

You should check out SharedPreferences. It's Android's way of persisting simple values. Or you could create a full database.

telkins
  • 10,440
  • 8
  • 52
  • 79
0

Use SharedPreferences to save small chunk of app data. Check the developer's website for this.

Also check out this Tutorial for step by step guide.

Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
-3

You can use local DB like SQlite for for your app.

  • 2
    Why are suggesting the use of SQLite ? Do you really think its feasible to use DB in this case ? – GrIsHu Oct 14 '13 at 05:46