SharedPreferences.Editor editor = getSharedPreferences("myPrefs", MODE_PRIVATE).edit(); editor.putString("username", "John"); editor.putInt("score", 100); editor.commit();
SharedPreferences settings = context.getSharedPreferences("myPrefs", 0); SharedPreferences.Editor editor = settings.edit(); editor.putInt("numRuns", numRuns+1); editor.commit();In this example, we retrieve an instance of SharedPreferences and create an instance of SharedPreferences.Editor to modify the shared preference file named "myPrefs". We add an integer value to the file under the key "numRuns" and commit the changes to the file. The SharedPreferences.Editor commit method belongs to the android.content package, which is a part of the Android SDK.