Storage option in Android (Shared Preferences)

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

  1. Shared Preferences
  2. Store private primitive data in key-value pairs.

  3. Internal Storage
  4. Store private data on the device memory.

  5. External Storage
  6. Store public data on the shared external storage.

  7. SQLite Databases
  8. Store structured data in a private database.

  9. Network Connection
  10. 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.
  2. Editor editsharedPreferences = sharedPreferences.edit();

  3. Add values with methods such as putBoolean() and putString().
  4. editsharedPreferences.putString(“Your Value”,”Your Value”);

  5. Commit the new values with commit()
  6. editsharedPreferences.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) {