Shared Preferences are used to store and retrieve data in between android activity’s  it is much helpful when data is to be used in more than one activity.

Initialization of a shared preference


here AndroidCoding is used to store the values and the same is used in another activity to retrive   Shared Preference can store and retrieve multiple types of data at once also, depending upon requirement we can use them i have listed out few types supported in shared preference.

  1. Boolean –> editor.putBoolean("key_name1", true); 
  2. Integer –> editor.putInt("key_name2", "int value");
  3. Float –> editor.putFloat("key_name3", "float value");
  4. Long –> editor.putLong("key_name4", "long value");
  5. String –>editor.putString("key_name5", "string value");

  As it provides wide range of possibilities in storing data it is much helpful, and all the data is store at one place and can be used as many times as possible.






Creating Main Activity File :

Creating activity_main.xml

  Adding edittext, textview and button for user to input text


Creating File

  Now we will be creating second screen where we will fetch the shared preference value and show it with help of a text-view.

Creating secondscreen.xml File :

  Now we will design second screen to show output

 secondscreen.xml :


  Adding to manifest file and no special permissions are required.

Output :

  sharedpreferences1   That’s it for this tutorial any queries comment below.

Show Buttons
Hide Buttons
Read previous post:
Android Tutorial on Grid-View