Professional Documents
Culture Documents
UNIT-III
Data Persistence
DATA STORAGE IN ANDROID
Persistence : Saving the state of an object in a stable storage area.
Android provides several storage options for Private and Public
data depending on space requirements
They are
1. Shared Preferences - stores private primitive data in terms of
key-value pair
2. Files
SQLite Databasestore
getPreferences(int mode)
Use if you need only one preferences file for your Activity.
Only one preferences file for an Activity – don't supply a name.
Calls method getSharedPreferences(String, int) passing in this activity’s
class name as the preferences name.
Preferences are not shared with other activities in the application.
SHARED PREFERENCE MODES
MODE_PRIVATE created file can be accessed only by the calling
application generally the only preference mode that you should use
boolean commit()
Internal storage is the storage of the private data on the device memory.
By default these files are private and are accessed by only your
application and get deleted , when user delete your application.
Writing file
In order to write some data in the file, call the openFileOutput() method
with the name of the file and the mode.
syntax is given below −
FileOutputStream fOut = openFileOutput("file name
here",MODE_WORLD_READABLE);
The method openFileOutput() returns an instance of FileOutputStream.
After that you can call write method to write data on the file.
String str = "data";
fOut.write(str.getBytes());
READING FILE