It’s been a long time since my last post but I am back now and I will try my best to write at least one or two posts each month.
It was quite busy here at Narbase, many projects, and many deadlines, and with lots of work comes lots of experience. In this post and the posts that follow I will write about the things I learned, the problems I faced and how I solved them and also from time to time I will write a tutorial on Android, Back-end and the tools that we use here in Narbase. So without any further ado lets get started.
Did you ever wonder where did WhatsApp store your messages history or that meme your friend has sent you the other day? or when you change your application settings, where these setting are stored? or even when you login into your account in twitter after entering your username and password and start using it, but when you open twitter the next time you will be logged in automatically; did you ask yourself how did the app know that you are already logged in? That means your app can store data in one way or another.
There are basically different techniques for data storage that are available for Android developers. In this post, I will talk to you about a couple of them and the rest will be in my next post.
Shared preferences are used when you don’t need to store huge amount of data and don’t need a structure to store them so you can store them in key-value pairs of primitive data types. The key must be a string and by using that key you can retrieve the value corresponding to that key. This value can be any type of primitive data such as integers, floats, booleans, strings, and longs. Shared preferences are private by default and usually used to store the application preferences such as preferred setting and if the user logged in or not. Shared Preferences are stored in XML files in a private directory.
Shared Preferences can be very limiting when you want to store data in your phone, or your data need a file system. So you can store app-private files on your device file system. Data stored in the internal storage of your device are private to your app by default and no other app can access them even the user cannot access them unless the user has root access but usually, the user doesn’t need to access them directly. The Android system provides a private directory for each app on the file system so you can organize your app files. Internal storage files related to your app will be removed if you uninstalled the app, so if you want to keep your photos you captured using your app you should consider storing them into the public external storage.
If you want to store some of your app data temporarily without storing it persistently you can use the cache directory to save the data. Like the internal storage each app has a private cache directory but be careful because the device can delete these files if the internal storage is low to recover some space but don’t rely on the system to clean up your cache and it’s your responsibility to maintain the cache files and keep them within a reasonable size such as 1 MB. Also, like the internal storage cache files will be removed if you uninstalled the app.
Those were two techniques for data storage in Android devices. Now we came to the end of the first part of this topic. In my next post, I will talk to you about the other two techniques. So that’s it for this post.
Till my next post.