Android Programming Lecture 7 Data Persistence
Android Programming Lecture 7 Data Persistence
What is 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.In other words,for a data store to be considered persistent,it must write to non- volatile storage. 2
What is 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. In other words, for a data store to be considered persistent, it must write to nonvolatile storage. 2
Saving Data in Android App data is private to the application Internal Storage:Store data on the device memory temporarily Shared Preferences:Lightweight mechanism to store private primitive data in key-value pairs in hard drive File:Open and save files on the device or removable storage SQLite Database:Store structured data in a private database Network Connection:Store data on the web Content provider is used to give the data to other apps
Saving Data in Android • App data is private to the application • Internal Storage: Store data on the device memory temporarily • Shared Preferences: Lightweight mechanism to store private primitive data in key-value pairs in hard drive • File: Open and save files on the device or removable storage • SQLite Database: Store structured data in a private database • Network Connection: Store data on the web • Content provider is used to give the data to other apps 3
Passing Temporal Data using Intent Intent intent new Intent(Activity1.this,Activity2.class); Activity 1 Bundle data new Bundle(); data.putstring("key_name","name"); data.putstring("key_age","age"); intent.putExtras(data); Intent intent.putExtra("key_id","id"); intent.putExtra("key_address","address"); startActivity(intent); /In Activity2.java Activity 2 /Retrieve the intent Intent intent getIntent(); /Retrieve the string data in the intent String name intent.getstringExtra("key_name"); String age intent.getstringExtra("key_age"); String id intent.getstringExtra("key_id"); String address intent.getstringExtra("key_address"); 4
4 Activity 1 Activity 2 Intent Intent intent = new Intent(Activity1.this, Activity2.class); Bundle data = new Bundle(); data.putString("key_name", "name"); data.putString("key_age", "age"); intent.putExtras(data); intent.putExtra("key_id", "id"); intent.putExtra("key_address", "address"); startActivity(intent); // In Activity2.java // Retrieve the intent Intent intent = getIntent(); // Retrieve the string data in the intent String name = intent.getStringExtra("key_name"); String age = intent.getStringExtra("key_age"); String id = intent.getStringExtra("key_id"); String address = intent.getStringExtra("key_address"); Passing Temporal Data using Intent
Data Persistence in Orientation When screen rotation is changed,the activity is destroyed and opened again How to store state information Store state:-onSaveInstanceState(Bundle) Read state:-onRestoreInstancestate(Bundle) This will store data only temporarily for app lifetime! o Data will be held in memory until the app is closed! Refer to the Android Developer Site: http://developer.android.com/training/basics/activity-lifecycle/recreating.html
Data Persistence in Orientation • When screen rotation is changed, the activity is destroyed and opened again • How to store state information • Store state: – onSaveInstanceState(Bundle) • Read state: – onRestoreInstanceState(Bundle) • This will store data only temporarily for app lifetime! o Data will be held in memory until the app is closed! 5 Refer to the Android Developer Site: http://developer.android.com/training/basics/activity-lifecycle/recreating.html
Saving Data in Android ·Internal Storage Shared Preferences ·File ·SQLite Database ·Network Connection 6
Saving Data in Android • Internal Storage • Shared Preferences • File • SQLite Database • Network Connection 6
Shared Preferences The SharedPreferences interface (in package android.content)provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types and strings. o similar to saving data in a Bundle Can be used to save the following data types o Boolean float o int long o String Set Shared preference data will persist across user sessions even if the application is killed
Shared Preferences • The SharedPreferences interface (in package android.content) provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types and strings. o similar to saving data in a Bundle • Can be used to save the following data types o Boolean – float o int – long o String − Set • Shared preference data will persist across user sessions even if the application is killed. 7
public class Example extends Activity /Create a preference file with the name specified public static final String PREFS_NAME "MyPrefsFile"; @Override protected void onCreate(Bundle state){ super.onCreate(state); /Restore the preferences //Get the preference with the name specified SharedPreferences settings getsharedPreferences(PREFS_NAME,0); /Read the stored data from the preference int data settings.getInt("key",defaultvalue); com.example.toml.sharedpreference 2015-09-0212:12dw0-x-x cache 2015-09-02 11:23 drwxrwx--x @Override 三ib 2015-09-0211:23 Irwxnwxrwx >/data/a... protected void onStop(){ 2015-09-02 1212 drwxrwx--x 目Filel.xml 1092015-09-0212:12 super.onstop(); -nw-n---- /Store the preferences /We need an Editor object to make preference changes. SharedPreferences settings getsharedPreferences(PREFS_NAME,0); SharedPreferences.Editor editor settings.edit(); editor.putInt("key",value); //Commit the edits! editor.commit(); 8
8 public class Example extends Activity { // Create a preference file with the name specified public static final String PREFS_NAME = "MyPrefsFile"; @Override protected void onCreate(Bundle state){ super.onCreate(state); . . . // Restore the preferences // Get the preference with the name specified SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); // Read the stored data from the preference int data = settings.getInt(“key", defaultValue); . . . } @Override protected void onStop(){ super.onStop(); // Store the preferences // We need an Editor object to make preference changes. SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putInt(“key”, value); // Commit the edits! editor.commit(); } }
Overview of Shared Preferences Examples of data stored in shared preferences: o user name -password o email address high score An application can have multiple sets of application preferences,where each set has a name Preferences can be stored at the activity level or the application level.In general,they are not shared outside the application. Application preferences are stored in XML files in the Android file system as follows: /data/data//shared_prefs/.xml
Overview of Shared Preferences • Examples of data stored in shared preferences: o user name – password o email address – high score • An application can have multiple sets of application preferences, where each set has a name. • Preferences can be stored at the activity level or the application level. In general, they are not shared outside the application. • Application preferences are stored in XML files in the Android file system as follows: /data/data//shared_prefs/.xml 9
Obtaining a SharedPreferences Object Two methods that return a SharedPreferences object: getSharedPreferences(String name,int mode) o Use if you need multiple preferences files identified by name. o Name is specified as the first parameter. o If a preferences file by this name does not exist,it will be created when you retrieve an editor. o Preferences can be accessed by all activities in the application. getPreferences(int mode) o Use if you need only one preferences file for your Activity. o Only one preferences file for an Activity-don't supply a name. o Calls method getsharedPreferences(String,int)passing in this activity's class name as the preferences name. o Preferences are not shared with other activities in the application. 10
Obtaining a SharedPreferences Object Two methods that return a SharedPreferences object: • getSharedPreferences(String name, int mode) o Use if you need multiple preferences files identified by name. o Name is specified as the first parameter. o If a preferences file by this name does not exist, it will be created when you retrieve an editor. o Preferences can be accessed by all activities in the application. • getPreferences(int mode) o Use if you need only one preferences file for your Activity. o Only one preferences file for an Activity – don't supply a name. o Calls method getSharedPreferences(String, int) passing in this activity’s class name as the preferences name. o Preferences are not shared with other activities in the application. 10