Unable to save general settings in Android

Now I am trying to save the variable when I close the application and return the variable when I open the application. I have no idea if I am doing this correctly. My variable is called count and wants to save and restore it. It is right? If so, why is it not working? If not, what do I need to change? (I explicitly use SharedPreferences)

protected void onPause(){ super.onPause(); SharedPreferences settings = getSharedPreferences(PREFS_COUNT, 0); SharedPreferences.Editor editor = settings.edit(); editor.putInt("count", count); editor.commit(); } @Override protected void onResume(){ super.onResume(); SharedPreferences settings = getSharedPreferences(PREFS_COUNT, 0); count = settings.getInt("count", count); } 
+7
variables android sharedpreferences
source share
1 answer

It looks correct, except that you have a constant:

 public static final String PREFS_COUNT = "MyPrefsFile"; 

announced at the beginning of your business. Here's all the Google documentation:

http://developer.android.com/guide/topics/data/data-storage.html#pref

It should work fine if you do it for sure.

+6
source share

All Articles