Click here to Skip to main content
12,453,672 members (62,127 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

19.9K views
1.9K downloads
5 bookmarked
Posted

Android:SharedPreference

, 6 Oct 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
How to use SharedPreference to store information in key-value pair in Android

Introduction

Here we see how to store a small amount of information using SharedPreferences APIs, and how to retrieve their values.

Background

Android SharedPreferences is useful when you want to save some data across applications.

Sometimes SharedPreferences is useful for login session. Once user successfully logs into application, then whenever the user opens the application, she/he not need enter username and password again to log in.

SharedPreferences provides a general framework to save and retrieve persistent data in key-pairs value of primitives data type. We can use SharedPreferences to save any primitives data type like strings, ints, booleans, floats, longs.

You can create Object of SharedPreferences using two methods:

  • getSharedPreferences(): Using this method, you can create multiple SharedPreferences and its first parameters in the name of SharedPreferences.
  • getPreferences(): Using this method, you can create a single SharedPreferences.

Using the Code

Here, we take an example of login form. When users enter their login credentials, and check the checkbox (i.e. Remember), the credentials store in the application, and when users open that application again, they not need enter their login information again.

To store information, we write:

SharedPreferences preferences = getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("username", username.getText().toString());
editor.putString("password", password.getText().toString());
editor.putString("logged", "logged");
editor.commit();  

Here we set Context.MODE_PRIVATE, so the file is accessible by only your application.

If you want to reset your preferences:

SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Satyam A.
Software Developer
India India
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 1 Pin
XOIII16-Oct-13 21:49
memberXOIII16-Oct-13 21:49 
GeneralMy vote of 5 Pin
ThatsAlok7-Oct-13 16:26
memberThatsAlok7-Oct-13 16:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160826.1 | Last Updated 7 Oct 2013
Article Copyright 2013 by Satyam A.
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid