Developing Android Applications with Adobe AIR [29]
Saving data is essential for a good user experience and for continuity. Saving should be considered when the application is about to exit, when it goes to the background, and at small intervals while your application is active.
To keep your audience engaged, avoid having them go over the same introductory steps every time they launch your application. Instead, bring them directly to the application’s core. Your application may be the scheduler of an event occurring over several days. Keep track of the date it was last used and display it first the next time the application is opened.
Persistent data may be an important component of your application, as in a game where the usernames and scores are saved and can be improved upon. If your game was interrupted in the middle of game play, keep track of its progress and display it in the same state the next time it is visible.
Data may be an essential part of your application if the user has the opportunity to build a library of information to manipulate and save over time. If your application is a utility for collecting a selection of images, a storage mechanism is the core of its functionality.
Internal or External Storage?
Let’s consider where to save data first. Data can be saved internally or externally.
Internally, File.ApplicationDirectory is the directory where your application and its assets are installed. AIR made this directory read-only because it is not writable on all systems. Instead, you can write to File.ApplicationStorageDirectory, the storage directory allocated to your application. Use this location to save fairly small amounts of data and information such as preferences and user settings. For example, your application data should not occupy more than a portion of the application’s full size.
If your application is removed, the data saved in storage is deleted along with it.
Users can erase the data by selecting Settings→Applications→Manage Applications→Application→Clear Data. They will be alerted to the consequences with a warning that reads, “All of this application’s data will be deleted permanently. This includes all files, settings, accounts, databases and so on.” Android provides the option to set allowClearUserData to false to prevent users from clearing data. At the time of this writing, this feature is not available in the Flash Professional and Flash Builder permissions panel.
Data can also be saved internally in the memory cache allocated to your application. To use this approach, create a temporary file or folder and save data in it. This is a good place to save noncritical information such as downloaded data files that may not have a lasting value, or temporary saved files. If your application is removed, the data saved in the cache is also deleted.
Users can erase the cache under Settings→Applications→Manage Applications→Application→Clear Cache. Android does not provide the option to prevent clearing the cache.
Externally, data can be saved on the device’s SD card under the File.documentsDirectory directory, also referred to as File.userDirectory or File.desktopDirectory. Use this approach for any relatively large amounts of data, such as images or video or temporary files. Create a directory with your application name to keep it distinct from other applications’ data.
Writing to the card requires a permission, which needs to be added to the descriptor file. If you don’t have this permission, AIR will throw a runtime error:
Before installing any data, make sure the user’s phone has an SD card: if (File.userDirectory) // proceeds with saving data You can use this approach as a way for one application to write data and another application to access that data. If your application is deleted, the data is not deleted automatically.