Skip to content

Instantly share code, notes, and snippets.

@ashutoshpw
Created May 7, 2018 06:29
Show Gist options
  • Save ashutoshpw/0cd6c1aeaf26b3f0d8485c57ffd92e40 to your computer and use it in GitHub Desktop.
Save ashutoshpw/0cd6c1aeaf26b3f0d8485c57ffd92e40 to your computer and use it in GitHub Desktop.
Android Shared Preference
public class MainActivity extends AppCompatActivity
SharedPreferences sharedpreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putString("key","value");
editor.commit();
// For Async Commit
//editor.apply();
// Read
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
String key = settings.getString("key", "DEFAULT_VALUE");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment