In this course dedicated to Android storage, you’ll study different ways to save (or persist) content in user's phones, 100% offline, without any need for the internet! Before we can get into the nitty-gritty of how to do this, let's review the different ways Android has to save app data on phones.
How do I store data in Android?
Storing data with shared preferences
In the first course of the Android curriculum, we covered SharedPreferences, which is a way of storing primitive, simple data. Basically, you store only variables (values), which you then retrieve using a single identifier (key).
This type of storage is called key-value storage. The variables (and their corresponding keys) will automatically be stored in an XML file directly on the user’s telephone. That file may be defined in private mode to allow only your app to access its content, or public if you want other apps to be able to access it.
Storing data with databases
To store structured data, Android natively offers the ability to use an SQLite database. This type of storage will enable you to organize and persist large quantities of structured data directly on the user’s phone, which can be worked on using SQL language.
We’ll be talking about using and implementing SQLite on Android (with Room) in greater detail in the second part of this course. 😁
Internal and external storage
To support file data storage (photos, videos, PDFs, etc.), Android devices have two storage spaces: internal and external.
These names come from the time when most Android devices had both a physical, non-volatile internal memory, and removable memory, such as an SD card. Today, removable memory is falling out of favor, and many devices now divide their physical storage space into separate partitions: internal and external memory.
Exactly! 😄 In fact, there are several differences between these two storage spaces:
Files stored in this space will always be available.
Files stored in this space will not always be available. That’s because they can be removed by the user at any time (either by transferring the files to another external storage device or, in some phones, by physically removing it via an SD card).
Files stored in this space can only be accessed by your application.
Files stored in this space can be accessed by anyone. Therefore, you have no control over them.
When the user uninstalls your app, the files will automatically be deleted from this storage space.
When the user uninstalls your app, the files will not be deleted from this storage space (unless you explicitly defined them to).
A few words on the internal storage space
By default, the files stored in the internal storage space will be private and can only be accessed by your app. This means they won't be visible to other apps or to the user (unless he or she has a phone with root access). 😊 The internal storage space can also be used if you want to temporarily save data in a special cache folder. So, if your user’s telephone is short on storage space (or if a cleaning program like Clean Master has gone through... 😋), the contents of this folder will automatically be erased to free up some space.
A few words on the external storage space
Files stored in the external storage space like Documents, Downloads, and Music are considered public and accessible to everyone. You have no control over what is stored in this space. You can simply define them as public (not be deleted when the user uninstalls your application) or private (deleted when the user uninstalls your application).
Okay, I get the theory, but how am I actually going to use it?
Understanding this basic information is critical if we're going to create a 100% offline app for our users. But first, we need an app! Enter SaveMyTrip!
Overview of the app SaveMyTrip
SaveMyTrip is the mini-app that we will develop throughout this course:
Its objective is to be able to easily organize your future trips, thanks to its fantastic travel diary as well as its interactive list of things to do, all accessible 100% off-line! 😀
You can download this mini-app SaveMyTrip as a nearly blank state, save for a few files:
Resource files: The resource files colors.xml, dimen.xml, strings.xml and styles.xml file are already preconfigured.
Layout files: To streamline the code found in the chapters, the XML design of the app's three screens was already performed. Feel free to check them out to absorb their info.
Classes of activities: The BaseActivity class, as well as the activity classes MainActivity, TripBookActivity, and TodoListActivity have already been created.
Utility class: The ItemClickSupport class used to more easily manage clicking on the RecyclerView has also been created.
External libraries: The Glide library is also installed via Gradle.
I have also chosen to organize the structure of the project by features, rather than MVC. But this doesn't change that fact that our architecture will still be MVC... 😉
And that's all! Now, the real fun begins with the very next chapter!
Shared preferences store primitive and unstructured data (variables).
Databases store structured data, which is manipulated via the SQL language.
Internal storage is:
Only accessed by your app.
Automatically deleted when a user uninstalls your app.
External storage is:
Not always available (can be removed by the user).
Accessible to anyone (so you don't have control over it).
Not automatically deleted when your app in uninstalled.
Okay, now that you know how data is stored...let's store some! 😉 Join me in the next chapter to work with external storage.