Persistent Data
Persistent Data
• 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
• 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
• 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.