Persistent Data in Android

    The capability to have data that the app uses or relies on to continue to be available regardless of changes to the app’s state as it moves through the app life cycle is vital to the user experience with the app—and for the app itself to be a useful tool. For this to occur, the data needs to persist through these life cycle changes. Android provides several ways in which the developer can make data persist. This chapter introduces you to three of these data persistence approaches. A significant amount of time will be spent on understanding and using the SQLite database system incorporated with Android, 

Developing The Android apps

 Preferences, Files, and Database:- The three approaches to data persistence discussed in this chapter are Shared Preferences , standard flat file input/output, and the SQLite database system. Each of these approaches provides capabilities that are relevant for different tasks in an app. Shared Preferences are often used for a limited set of data that represent user choices about the way they want the app configured. They may also be used for other data that needs to persist across life cycle changes. Flat files are useful for backing up data and transmitting to other users. Finally, databases are the workhorses for data manipulation, storage, and retrieval. Developing an understanding of where, when, and how to use these data persistence approaches is very important to effective development of an Android app.  

 Preferences Preferences are implemented through use of the SharedPreferences class. A SharedPreferences object can be used to store primitive data (for example, integers and strings) in a key/value pair. Each value has its own key for storage and retrieval of that data. SharedPreferences are stored in memory private to the app and will persist as long as the app remains installed on the device. App upgrades will not impact the values stored with  Shared Preferences.

 Files:-  Files are written and read as a stream of bytes. This means that to the Android system, a file is one thing. It does not have parts, such as different objects, within it. The advantage to this is that the system stores the data efficiently and does not have to worry about what data is stored within the stream. Thus, many kinds of data can be stored in a file. The disadvantage is that it is up to the developer to code the reading and writing of the file so that the data can be used appropriately when it is needed. For example, the developer can embed XML in the stream to identify the different types of data. Another approach is to embed commas in the file to distinguish different pieces of data. However, in either case, the user of that file must know its structure to use it correctly. 

 Database:- Android supports the use of SQLite databases. SQLite is a fully functional relational database management system (RDBMS) that can integrate into any host application. It does not require an independent server process to execute. A relational database system allows the developer to give meaning to the data stored within it by separating the data into tables (for example, a customer table and an order table. Each table will hold data pertinent for each instance of whatever is stored in the table (for example, data for each customer). SQLite also provides capabilities for retrieval and manipulation of the stored data through the use of queries written in Structured Query Language (SQL). 

 Android Versus iOS: Data Persistence Android and iOS offer essentially the same three types of data persistence mechanisms discussed in this chapter. The functionality provided by SharedPreferences in Android is provided by the NSUserDefaults object in iOS. File input and output is also provided in iOS. Finally, iOS also implements SQLite databases in a very similar manner to Android. iOS does offer a storage solution called Core Data that offers an object-oriented approach to storing data, but this is usually added on top of a SQLite database. Although in all cases the code has different commands because the programming languages are different.

  Creating the Database The MyContactList app uses a simple, one table database to provide the data storage and manipulation functionality described in C hapter 2 , “App Design Issues and Considerations.” Two new classes will be created to provide the database functionality. One class is used to create, modify, and delete the tables included in the database. The other class is used for data access. It provides methods to open and close the database and the queries used to store, access, and manipulate the data in the tables. Learn more Android Training

  Create the Database Helper Class The recommended approach to using SQLite in an Android app is to create a Database Helper class whose only function is to provide for the creation, modification, and deletion of tables in the database. The new class is defined as a subclass of the SQLiteOpenHelper class. Much of the required functionality for working with databases is inherited from the SQLiteOpenHelper class.

    1. Right-click com.example.mycontactlist in the src folder of the Package Explorer.

2. Select New > Class and enter ContactDBHelper as the name of the new class. 

 3. Type the code into the new class. You will have to import many of the objects after you have entered the code. 

 Using the Database The three classes, Contact , ContactDBHelper , and ContactDataSource , are used in the ContactActivity class to implement the saving of contact data to the database. This will require implementing several new methods and modifying some existing methods. Because the methods to retrieve contacts have not been implemented yet, the update functionality will be only partially implemented at this time. 

 The first step is to provide an association between the ContactActivity class and a Contact object. This is implemented by declaring a private variable in the ContactActivity class. Enter the following code after the class declaration and before the onCreate method: private Contact currentContact ;

Next associate the currentContact variable with a new Contact object by entering the following code as the last line in the onCreate method: currentContact = new Contact();

Notice that while you are using a new object ( Contact ) in the ContactActivity class, you do not have to import it. That’s because you created the Contact class as a part of the com. example.mycontactlist package. Android already knows about this class, so it does not have to be imported.

The final step in modifying existing code is to add a line of code in the didFinishDatePickerDialog method to store the selected birthday in the Contact object. Add the following line of code as the last line of code in that method: currentContact .setBirthday(selectedTime);

This code uses the Contact class’s setBirthday method to assign the date selected in the custom dialog to the currentContact object.

  Capture User-Entered Data The first new method needed is used to capture the user data as it’s typed and store it in the currentContact object. The method itself does not capture the data. Rather, it sets up listeners on all the EditText s where data can be entered. If the text changes, the listener then executes the code to set the attribute that holds the code in the currentContact object. The method is called in the onCreate method of the ContactActivity so that the listeners are ready to go when the ContactActivity is ready for input. To start, enter the following line of code after all the other init methods in the onCreate method: initTextChangedEvents();

Next, create a new method in the ContactActivity class called initTextChangedEvents() . Place this method after the other init methods currently in the class and follow  Android

 Using Preferences Shared Preferences are an easy way to store bits of information that need to persist over the life cycle of an app. In the My Contact List app, preferences are set in the Contact Settings Activity. This activity is developed so that you can learn how to use Shared Preferences . To do so, the layout is first coded and then the Java file is edited. Become  An Android Developer follow android app development course


Data that exists beyond the execution of the app is said to persist. In this chapter you learned several methods to make data persist in your app. You also learned how to get that persistent data for use in the app. A SQLite database is used to store complex data. 

  Modified On Sep-07-2019 12:21:42 AM

Leave Comment