0

I've asked a question like this before but the answers did not really help. If the user has changed the settings how do i save it so when the application is killed the changes are still saved. I have tried to use and save an int when the setting is changed, but when i go back, and then back to the page, the setting is back to normal. How do i save the setting with using shared Preferences or something?

Thank You

dizazta
  • 77
  • 2
  • 11
  • You should share the code that you're currently trying to use to store/retrieve the data. Also take a look, if you haven't already, at the code/write-up here: http://developer.android.com/guide/topics/data/data-storage.html#pref . Edited to add another link:http://developer.android.com/reference/android/preference/PreferenceActivity.html – Ross Aiken Jul 25 '12 at 01:20

1 Answers1

2

You want to use shared preferences http://developer.android.com/reference/android/content/SharedPreferences.html

To store it:

private void putValue(String name, int value){
    SharedPreferences sp = getSharedPreferences("sharedPreferences", 0)
    SharedPreferences.Editor prefEditor = sp.edit()

    prefEditor.putInt(name, value);
    prefEditor.commit()
}

to get it:

private void getValue(String name, int defaultValue){
    SharedPreferences sp = getSharedPreferences("sharedPreferences", 0)
    return sp.getInt("Name", defaultValue);
}

You can call in onPause

@Override
public void onPause(){
    super.onPause();
    putValue("IntValue", value);
}

An then onResume()

@Override
public void onResume(){
    super.onResume();
    value = getValue("IntValue", 0);
}
FabianCook
  • 20,269
  • 16
  • 67
  • 115
  • thanks, but what i'm doing is creating a tick if the item has been purchased. So when the item is clicked i set the alpha of the tick to 255 to show it has been purchased, however when i go back and then forward to it again its gone, sorry for the trouble – dizazta Jul 25 '12 at 01:10
  • Well save the alpha value and then put the alpha value when you load it. – FabianCook Jul 25 '12 at 04:27