Android Permanently Store and Retrieve Data Using Shared Preference ArrayList

Storage of Data is one of the important aspect to learn when developing mobile app, In this app We shall to learn how we can Android Permanently Store and Retrieve Data Using Shared Preference, Share Preference can be used to store a various types of data but in this case we are going to store ArrayList.

  • Let’s get started
  • Creating a new android studio project

  • Open your android studio.
  • Click File => New => Project => Empty Activity => Give a name, I have named it “DataStorage“.
  • Click next and finish.
  • Creating Arralist of Data

  • We are going to store fruit names in this arraylist.
  • Create an arraylist of string in your MainActivity.java class
  • Adding Items in ArrayList

  • To add data to our arraylist use the following.
  • Storing our ArrayList in SharedPreference

  • To store array of data in sharedpreference we need to serialize that array.
  • To serialize we shall use a standard class that is provide by apache to serialize and deserialize array.
  • Create java class and name it ObjectSerializer
  • Copy and paste the code below in ObjectSerializer.java.
  • I have customized the class to easy debugging, You can find the original class at apache
  • To serialize our ArrayList data use the code below.
  • Retrieving stored ArrayList from shared preference

  • Create an Empty arraylist which we will store retrived data.
  • Showing Retrieved Data

  • Toasting retrieved data.
  • The whole code for MainActivity is.
  • That’s all, Run your app and you will see the output below.
  • Android Permanently Store and Retrieve Data Using Shared Preference

  • To download source code use the link below
  • Download source code
  • If you need more help or have a question do comment.