Before moving on to your question, we briefly explain two types of storage:
Cache
This is a folder specific to the application on the file system. The purpose of this directory is to store temporary data that your application may need for sessions between sessions, but it is not necessary to have them forever. You usually access this directory with Context.getCacheDir() . This will appear as a "cache" in the settings of your application.
Files
Like the cache folder, your application also has a directory of applications for storing files. Files in this directory will exist until the application explicitly deletes them or the application is deleted. You typically access this directory using Context.getFilesDir() . This may appear as various things on the application information screen, but in your screenshot this is βUSB storage dataβ.
NOTE. If you want to explicitly place it on an external medium (usually an SD card), you can use Context.getExternalFilesDir(String type) . p>
Difference
Both directories are specific to your application only (other applications do not have access). One of the differences between the cache directory and the files is that if the system becomes smaller on storage, the first place it is going to free resources belongs to your cache directory. The system will not clear data from the file directory. Another difference is that the cache directory can usually be cleared manually from the application information screen. Normally, the file directory can also work, but clearing the file directory also clears the cache directory.
Which one am I using?
It depends on how important the data is compared to the lifetime of your application. If you only need data for one session, and you doubt that you will ever need to use this data again, then do not use it. Just keep it in your memory until you need it. If you suspect that you will need to reuse data between multiple sessions, but do not need to keep a paper copy, use the cache directory. If you must have this data no matter what, or if it is quite large data that requires permanent storage, use the file directory. Here are some examples I can think of:
- Cache - Recently Opened Email
- After opening, cache the data, so when the user wants to read this email again, he downloads instantly, instead of using the network again to get the same data. I do not need to keep this forever, because ultimately the user will be finished by email.
- Files - an application downloaded from email
- This is the action of a user who says "I want to save this data so that I can restore it when I need it." So put it in the file directory as I never want to delete this file until the user wants to delete it.
When do I need to clear the cache directory?
From Context.getCacheDir() javadocs:
Note. You should not rely on a system deleting these files for you; you should always have a reasonable maximum, for example 1 MB, for the amount of space that you consume with cache files, and trim these files when this space is exceeded.
It uses an example of 1 MB, but may or may not be reasonable for your application. Despite this, you need to set a hard maximum. The reason for this comes down to developing a responsible application. So when should you check? I would recommend checking every time you want to put something in the cache directory. Here's a very simple cache manager:
public class CacheManager { private static final long MAX_SIZE = 5242880L;
Of course, this can be an expensive operation, so you should plan on caching in the background thread.
In addition, it can be as difficult as you need. In my example, I assume that all cached files are located in the root of the cache directory, so I do not check for potential subdirectories. The process of deleting files can also become more complicated, for example, deleting files by the oldest access date.
One thing to keep in mind when deciding on data caching is that you always need to plan so that your cached data no longer exists. Always follow a routine to retrieve data by external means when your cache does not have it in storage. Similarly, always check your cache before retrieving data from the outside. The goal of the cache is to reduce network activity, lengthy processes, and provide a flexible user interface in your application. So use it responsibly :)