Storage option in Android (Shared Preferences)

Posted on by By Nikhilesh, in Miscellaneous | 0

Android has several option to store persistent data.Your data storage options are the following:

    1. Shared Preferences

Store private primitive data in key-value pairs.

    1. Internal Storage

Store private data on the device memory.

    1. External Storage

Store public data on the shared external storage.

    1. SQLite Databases

Store structured data in a private database.

    1. Network Connection

Store data on the web with your own network server.

It depends on developer which one is suitable for uer. We only look to shared preferences storage information.
If you want to use any other option then goto

Using Shared Preferences
The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

  1. getSharedPreferences() – Use this if you need multiple preferences files identified by name, which you specify with the first parameter.
  2. getPreferences() – Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don’t supply a name.

To write values perform the following steps:

    1. Call edit() to get a SharedPreferences.Editor.

Editor editsharedPreferences = sharedPreferences.edit();

    1. Add values with methods such as putBoolean() and putString().

editsharedPreferences.putString(“Your Value”,”Your Value”);

    1. Commit the new values with commit()


Note: sharedPreferences is an object of class SharedPreferences;

To read values, use SharedPreferences methods

  1. getBoolean(): sharedPreferences.getBoolean”yourkey”, “defaultValue to return if not exits”);
  2. getString(): sharedPreferences.getString(“yourkey”, “defaultValue to return if not exits”);

public class MyCustomListView extends ActionBarActivity {
	ArrayList array_list = new ArrayList();
	MyCustomAdapter myCustomAdapter;
	SharedPreferences sharedPreferences;
	protected void onCreate(Bundle savedInstanceState) {
            sharedPreferences = getSharedPreferences("anyName", Context.MODE_PRIVATE);
            // check if shared preferences exists
            if(sharedPreferences.contains("Book_Name") && sharedPreferences.contains("Book_Author")) {                    
                Bundle dataBundle = new Bundle();
                dataBundle.putString("book_Name",sharedPreferences.getString("Book_Name", "") );
                dataBundle.putString("book_Author", sharedPreferences.getString("Book_Author", ""));
                Intent intent = new Intent(MyCustomListView.this,Test_activity.class);
	    ListView listview = (ListView) findViewById(;
	    listview.setOnItemClickListener(new OnItemClickListener() {        
                public void onItemClick(AdapterView arg0, View view, int arg2,long arg3) {
                    // select your item and store in shared preferences             
                    Editor editsharedPreferences = sharedPreferences.edit();
                    // find text value of selected item
                    String book_name  = (String) ((TextView) view.findViewById(;
                    String book_author =(String) ((TextView) view.findViewById(;
                    // edit shared preferences
                    //commit shared preferences
	public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
	public boolean onContextItemSelected(MenuItem item) {

Best Open Source Business Intelligence Software Helical Insight is Here


A Business Intelligence Framework

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments