0% found this document useful (0 votes)
63 views13 pages

Data Persistence Chapter 6

This document discusses data persistence in Android applications. It explains that data persistence allows data to survive after the process that created it ends by saving data to non-volatile storage. It then provides examples of using SharedPreferences to save simple key-value data that will be loaded reliably later, including saving and retrieving class objects using GSON. Code snippets are given for setting up buttons and layouts to load, display, modify, and save preference values.

Uploaded by

Not Sure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views13 pages

Data Persistence Chapter 6

This document discusses data persistence in Android applications. It explains that data persistence allows data to survive after the process that created it ends by saving data to non-volatile storage. It then provides examples of using SharedPreferences to save simple key-value data that will be loaded reliably later, including saving and retrieving class objects using GSON. Code snippets are given for setting up buttons and layouts to load, display, modify, and save preference values.

Uploaded by

Not Sure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Data Persistence

PERSISTENCE IS "THE CONTINUANCE OF AN EFFECT AFTER ITS CAUSE IS REMOVED".


IN THE CONTEXT OF STORING DATA IN A COMPUTER SYSTEM, THIS MEANS THAT THE
DATA SURVIVES AFTER THE PROCESS WITH WHICH IT WAS CREATED HAS ENDED.
Data persistence

 Data persistence involves saving data in a non-volatile storage system so that the


data's value can be retrieved reliably later .
SAVING AND LOADING USER
PREFERENCES

 Android provides the SharedPreferences object to help you save simple application
data. Using the SharedPreferences object, you can save the data you want through the use
of key/value pairs — specify a key for the data you want to save, and then both it and its
value will be saved automatically to an XML file
TRY IT OUT: Saving Data Using the
SharedPreferences Object

 You can use gson.jar to store class objects into SharedPreferences. You can download
this jar from google-gson
 Or add the GSON dependency in your Gradle file:

 implementation 'com.google.code.gson:gson:2.8.8'
 Creating a shared preference:

 SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);


To save:

 MyObject myObject = new MyObject;


 //set variables of 'myObject', etc.

 Editor prefsEditor = mPrefs.edit();


 Gson gson = new Gson();
 String json = gson.toJson(myObject);
 prefsEditor.putString("MyObject", json);
 prefsEditor.commit();
To retrieve:

 Gson gson = new Gson();


 String json = mPrefs.getString("MyObject", "");
 MyObject obj = gson.fromJson(json, MyObject.class);
main.xml fi le

 In the main.xml fi le, add the following code in bold (replacing the existing TextView):
 <?xml version=”1.0” encoding=”utf-8”?>
 <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
 android:layout_width=”fill_parent”
 android:layout_height=”fill_parent”
 android:orientati ion=”vertical” >
main.xml fi le

 <Button
 android:id=”@+id/btnPreferences”
 android:text=”Load Preferences Screen”
 android:layout_width=”fill_parent”
 android:layout_height=”wrap_content”
 android:onClick=”onClickLoad”/>
main.xml fi le

 <Button
 android:id=”@+id/btnDisplayValues”
 android:text=”Display Preferences Values”
 android:layout_width=”fill_parent”
 android:layout_height=”wrap_content”
 android:onClick=”onClickDisplay”/>
 <EditText
 android:id=”@+id/txtString”
 android:layout_width=”fill_parent”
 android:layout_height=”wrap_content”/>
main.xml file

 <Button
 android:id=”@+id/btnModifyValues”
 android:text=”Modify Preferences Values”
 android:layout_width=”fill_parent”
 android:layout_height=”wrap_content”
 android:onClick=”onClickModify”/>
 </LinearLayout>
package

 Add the following lines in bold to the UsingPreferencesActivity.java fi le:


 package net.learn2develop.UsingPreferences;
 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 public class UsingPreferencesActivity extendds Activity {
package

 /** Called when the activity is first created. */


 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 }

 public void onClickLoad(View view) {


 Intent i = new Intent(“net.learn2develop.AppPreferenceActivity”);
 startActivity(i);
 }
 }

You might also like