How can I save and load variables constantly ? I find the Shared-Preferences but dont understand it well. For example I got a variable which is called
int global_coins
How can I load or save it ? Thank You !
How can I save and load variables constantly ? I find the Shared-Preferences but dont understand it well. For example I got a variable which is called
int global_coins
How can I load or save it ? Thank You !
You would do something like this to store a variable:
SharedPreferences sp = context.getSharedPreferences(context.getString(R.string.shared_preferences_file), 0);
Editor editor = sp.edit();
editor.putInt(context.getString(parameterRid), value);
editor.commit();
And something like this to read a variable:
SharedPreferences sp = context.getSharedPreferences(context.getString(R.string.shared_preferences_file), 0);
return sp.getInt(context.getString(parameterRid), defaultValue);
You should really read the documentation for SharedPreferences, it's really simple to understand and the official guide to data storage is very useful.