0% found this document useful (0 votes)
6 views

Persistent Data

The document outlines various methods for persisting data in Android applications, including SharedPreferences, Internal and External Storage, SQLite Databases, and Network Connections. Each method has its advantages and disadvantages, such as data accessibility, storage capacity, and persistence across app sessions. It also discusses what types of data are suitable for each storage option and the implications of using them.

Uploaded by

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

Persistent Data

The document outlines various methods for persisting data in Android applications, including SharedPreferences, Internal and External Storage, SQLite Databases, and Network Connections. Each method has its advantages and disadvantages, such as data accessibility, storage capacity, and persistence across app sessions. It also discusses what types of data are suitable for each storage option and the implications of using them.

Uploaded by

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

Module 5 Syllabus

▪Persisting data - Files, saving state and preferences - saving application


data,creating, saving and retrieving shared preferences, preference framework
and preference activity, preference layout in XML,
▪native preference controls,preference fragments, preference activity, persisting
the application state, includingstatic files as resources,
▪Working with file system,
▪SQLLite - SQLLite types,database manipulation using SQLLite, SQL and
database centric data model for Android, Android database classes.
Persisting data
• Persistent data denotes information that is infrequently accessed and
not likely to be modified.
• Android provides several options for saving data persistent
applications
• The solution that is chosen will depend on specific needs, such as
whether the data should be private to their application or accessible to
other applications (and user) and how much space data require.
• Data storage options in android are the following
• SharedPreferences:store primitive private data on key-value pairs.
• Internal Storage: store private data in the device memory. (With object persistence)
• External Storage: store public data on the shared external storage.
• SQLite Databases: store structured data in a private database.
• Network connection:- store data on the web with own network server
Saved Instance State

• Saved Instance state is the very first scene when you are storing and retrieving
the data in your application.
• onSaveInstaceState() method is called when an activity is destroyed which takes
a Bundle as a parameter
• This bundle is a collection of a key-value pair in which we can store the state of
every view in the bundle let it be Edittext, TextView, CheckBox or the scrolled
position of list view or recycler view.
• When the application is restarted then onRestoreInstanceState() is called to
which we provide this bundle and it automatically restores the activity to the
state in which it was closed. This agenda enhances the user experience
• what to save?
• texts in edit texts,instances of ImageViews,the position of scroll
views,the current playback time of videos,checks of
checkboxes,currently selected view
• what not to save?
• Images,videos,files,Internet downloads

• Advantages of saved instance state


• State of the activity is restored if the activity is closed.
• saving data is quick and simple in key-value pairs.
• useful when device orientation is changed.
• Disadvantages of saved instance state
• Data stored is not persistent i.e; data is lost if the device is restarted.
• Do not save data if the app is closed via finish() method.
1.SHARED PREFERENCES
• Shared preferences provide a quick interface to save and retrieve small
unstructured data in a key-value pair.
• You have a string key, and the corresponding primitive value for that
key: boolean, String, int, float or long.
• The data in shared preferences persist across user session even if
application is killed and restarted or the device is rebooted.
• Android platform stores the applications shared preferences in an XML
file with private access to the application.
• An application may have one(default shared preference of application)
or more Shared preference files(defined with unique names).
• Shared preference data is lost when the user uninstalls the application or
clears the application data from settings.
What to save and what not?
• To point out what kind of data to store in shared preference consider the
example of a game application.
• Every time the user logs in to the application user should be able to see
the username, the last level he was in coupled with ,the high score that
user has made.
• Also, the application has to maintain user settings preferences, if the
user has turned on/off the music of the game or notifications.
• At the same time, there are things that shared preferences are not capable
of storing.
• When it comes to storing of large data then shared preference is not the
right choice. It fails to store large data.
• Do not opt for shared preferences when you need to store Images,
videos, Audio files, large text files, spreadsheets or presentations.
Advantages of Shared Preferences
• Works on the key-value basis. Simply provide the key and get
corresponding value.
• Just a few lines of code required to manipulate data.
• Data is private to your application
• Useful when need to store user preferences.
• When needed, you can share data among applications.
• Reading and writing data is easy and fast
• Unless the application is uninstalled or data is cleared from settings,
data persists in the application.
Disadvantages of Shared Preferences

• Cannot store a large amount of data.


• Cannot store Image, Audio or video data.
• Fails to store structured data.
• Storing huge amount of data in memory affects the speed and
efficiency of the application.
2.Internal File storage

• When the data is larger in amount or the data type is not primitive, we store the data
in device storage.
• Every Android device’s file storage area is divided into two parts: Internal storage and
External storage.
• Internal storage is a built-in-non-volatile memory which is always available on the
device.
• The internal storage directory is private to your application and the data cannot be
accessed by other applications.
• Moreover, you may store text and binary data like images in your device’s internal
memory.
• The data saved in the internal storage of the device is removed as soon as the
application is uninstalled from the device.
• File class and its various methods are used to store and retrieve the data in internal
file storage.
• what to save?
• large text files,spreadsheets or CSV files,Image thumbnails
• What not to save?
• Large audio files,video files,files larger than total 1mb.
• Advantages of internal file storage
• Data access is private
• neither the user nor other applications can access the data in
internal storage.
• Internal storage is always available on the device.
• Read and write data do not require user permissions.
• Disadvantages of internal file storage
• Uninstalling the application removes data internal storage.
• cannot store a large amount of data.
3.External file storage
• External storage is a removable storage medium like an SD Card.
• Today a lot of devices do not support SD card.
• However, the permanent storage is divided into internal and external storage
modules.
• You will need user permissions to access(read and write) external storage.
• The external storage is however by default public in access.
• Any application or user can see and manipulate the data in external storage.
• File class and its methods to save, retrieve and delete the data in external file
storage.
• It stores a large amount of data with any data type.
• what to save?
• Large size images,Audio and video files,downloads from the
internet,Document files
• what not to save?
• Any data that you do not want to share with other applications or the user.
• Advantages of external file storage
• Store huge amount of data.
• can store any type of data.
• data remains in the storage even if the application is uninstalled or data is
cleared from settings.
• Disadvantages of external file storage
• Data is world readable. In general, any application or user can read the data.
• Not always available as the user may remove the SD card from the device.
4.SQLite Database

• SQLite is an RDBMS library that implements SQL database engine which


is:
• self-contained (no other component required)
• serverless (no server backend)
• zero-configuration (does not need to configure)
• transactional (changes within a single transaction in SQLite)
• stores data in a table of rows and columns.
• Transactions in SQLite are ACID and the system crashes do not affect
them.
• SQLite database is a premium choice when you need to manipulate
structured data frequently.
• Query
• We use a query language to interact with the database. Queries can be
complex but basically, we use the following operations:
• inserting data
• deleting data
• updating data
• retrieving data
• Cursors
• A Cursor is an object interface that provides random read-write access to
the result set returned by the database query.
• In other words, A cursor is a pointer to the rows of the table of the
database.
• A cursor class provides methods to run the cursor throughout the structured
data, and methods to retrieve data from columns of each row.
• what to save?
• Related data like a phone contact,user statistics,update, delete and view
flags,URLs and URI
• what not to save?
• Images as well as media files,non-relational data,single key-value
pair,Document files
• Advantages of SQLite database
• Serverless hence, no configuration required
• able to control redundancy of data
• maintains the integrity of stored information
• can share data using content providers when needed
• integrated with Android studio
• Disadvantages of SQLite database
• Cannot store media files
• data loss on removal of the application
5. Network connection

• The network connection can be used to retrieve and send data to a back-
end server of your application.
• In this case, backend server maintains the database of your application.
• There are several APIs through which you can access this data. Every
time you have to hit an API URL to receive or send data.
• To perform network operations we have the following packages that we
can use:
• java.net.*
• android.net.*
• Every time to perform a network operation you have to open an HTTP
connection and provide the method as GET or POST.
• After that, you may be able to connect to the API URL and exchange the
data.
• what to save?
• Media files,Documents,User preferences
• what not to save?
• Application settings
• Any data that you may need offline
• Advantages of Network connection
• Even uninstalling the application do not affect the data.
• Can store any amount of data
• Disadvantages of network connection
• Internet connection is must to transfer data.
• If backend server crashes then all the data may lose.

You might also like