Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data persistence How to save data using SharedPreferences, Files, and SQLite database 1Data persistence.

Similar presentations


Presentation on theme: "Data persistence How to save data using SharedPreferences, Files, and SQLite database 1Data persistence."— Presentation transcript:

1 Data persistence How to save data using SharedPreferences, Files, and SQLite database 1Data persistence

2 SharedPreferences Saving simple application data like – Text strings – Preferred font size, background color, etc. – Example: PreferenceMenuExample Reading preferences, example SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE); float fontSize = prefs.getFloat(FONT_SIZE_KEY, 12); // default value: 12 editText.setText(prefs.getString(TEXT_VALUE_KEY, "")); // default value: empty string Writing preferences, example SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE); // MODE_PRIVATE: Only accessible to this application SharedPreferences.Editor editor = prefs.edit(); editor.putFloat(FONT_SIZE_KEY, editText.getTextSize()); editor.putString(TEXT_VALUE_KEY, editText.getText().toString()); editor.commit(); OR BETTER editor.apply(); 2Data persistence

3 SharedPreferences: Obtaining the object The way to get access to a SharedPreferences object it through the method – SharedPreferences getSharedPreferences(String name, int mode) String name: The name of the preferences object Int mode: Decides which application can access the object – Factory method design pattern Modes – MODE_PRIVATE Only accessible to the creating application Any Activity in the creating application can read and write to the object – MODE_WORLD_READABLE Any application can read the object. Only the creating application can write to the object – MODE_WORLD_WRITEABLE Any application can read and write to the object Data persistence3

4 SharedPreferences: Some methods Int getInt(String key, int defaultValue) General – Type getType(String key, Type defaultValue) – Type: float, long, int, String, etc. boolean contains(String key) Map getAll() SharedPreferences.Editor edit() Data persistence4

5 SharedPreferences.Editor: some methods SharedPreferences.Editor putType(Type element) – Type: float, long, int, String, etc. SharedPreferences.Editor remove(String key) – Removes one entry SharedPreferences.Editor clear() – Removes all entries Void apply() + boolean commit() Data persistence5

6 Using SharedPreferences In most cases a SharedPreferences object should only be accessible in a single application – MODE_PRIVATE Can be used to hold information about the users preferences or personal data – Background color, birth day, name etc. – Username + password for easy login. Security?? Can be used to transport data between Activities – An alternative to putting the data into an Intent object. – However, data is going to last longer with SharedPreferences You can find the preferences file – From Android Studio open Device Monitor → File Explorer → data → data → appName → shared_prefs – To see the contents of the file: Pull a file from device (small red/black icon top-left) Data persistence6

7 Files The basic file API classes from the ordinary Java package java.io – FileInputStream, InputStreamReader, BufferedReader, etc. However, opening a file is different – FileInputStream fis = openFileInput(someName, MODE_PRIVATE); – openFileInput is yet another method from the Activity class You can create files (text or binary) and read them Files can be stored – On the device’s internal store – On an SD memory card For transportation to another device Usage – High score lists, pictures, etc. Example: FilesExample + FilesExampleAsyncTask Data persistence7

8 Databases: SQLite Android comes with a small scale relational DBMS called SQLite – http://sqlite.org/ http://sqlite.org/ Uses ordinary SQL The database file is stored in the folder – /data/data/ /databases – Can be seen in the debugger tab File Explorer File Explorer can be opening from the menu Window -> Show View A database is only accessible to the application which created the database – Secure – The database must be created by the application using it Usually you will create the database the first time you use the application – No DBMS tools/user interfaces for your phone! Example: DatabaseExample Data persistence8

9 Database programming practice It’s good practice with SQLite (and any other DBMS) to put the database related code in a separate class – This calls is usually called DBAdapter This class is NOT an Activity, but a plain Java class Data persistence9

10 SQLite database versions When you create a SQLite database you should must give it a version number – Start using version 1. You cannot alter the tables, etc. In the database What you can do is that you can create a new database with a new version number – And all your old data is lost Data persistence10

11 Foreign keys SQLite claims to have support for foreign keys starting from version 3.6.19 – http://www.sqlite.org/foreignkeys.html http://www.sqlite.org/foreignkeys.html – http://www.sqlite.org/faq.html#q22 http://www.sqlite.org/faq.html#q22 Foreign key checking must explicitly be switched on – PRAGMA foreign_keys = on http://www.sqlite.org/foreignkeys.html#fk_enable Data persistence11

12 SQLite, a few more words SQLite can run on ordinary computers as well as on mobile devices. SQLite comes with a lot of options – Some of which can make SQLite run faster Data persistence12


Download ppt "Data persistence How to save data using SharedPreferences, Files, and SQLite database 1Data persistence."

Similar presentations


Ads by Google