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