About Cindy Potvin

Cindy is a programmer with over 5 years of experience developing web applications. She also works on Android applications and share her knowledge via her blog and on Twitter.

Introduction : How to save data in your Android application

This is the first post in a series explaining the various ways to save data and application state in an Android application. There are many mechanisms in the Android SDK that can be used to save data, and it is something confusing to decide which one to use and when the operation should be triggered. One of the first and easiest things you’ll want to do when building your application is keeping data entered by the user and explicitly saved. Here are the most commonly used persistent storage modes :
 
 
 
 

  • The most basic way to save data is to save it to a file on the external storage of the device. This type of storage is accessible by all the other applications and by the user. It can also be seen when the device is mounted as external storage. The files will be kept even if your application is uninstalled unless you get the folder for which to save the files using the GetExternalFileDir() method. This is the best for files that are edited by the user and should be kept even if your application is removed, for example pictures and documents.
  • You can also save files to an internal storage that is private for your application. In that case, the files are not accessible by the user or by other applications and will be deleted when the application is uninstalled. This is best if you want to save simple data to a file while avoiding the overhead of managing a database.
  • Also, you can save user preferences as key/value pairs with the Preference API to create a SharedPreferences file. You can create an activity to save preferences using the PreferenceActivity or an activity that hosts a PreferenceFragment, both containing Preferences object in their layout instead of view objects.
  • Finally, if you have data with a complex structure to save, each application has its own private SQLLite database to persist data. This data will be destroyed if the application is uninstalled.

Also, there are many cases where the current state of the application needs to be saved because an event was triggered. For example :

  • When the device is rotated, the current activity is recreated so the layout can be adjusted.
  • When your application is sent to the background by the user or by another application, for example if a phone call is received. Also, if another activity in your application is by the user, the current activity is also sent to the background. An activity that is sent to the background can be recycled if the device needs more memory and is recreated when needed again.
  • When the application is explicitly closed by the user.

In those cases, you may need to save the data the user entered in a permanent data store. When the activity is sent to the background or on its way to be destroyed, the onPause event is always raised, followed by the onStop event if the IU is completely hidden, so saving persistent data is best done during the onPause event. Later on, when the user accesses the activity again, the onRestart, onStart and onResume events will be raised if the activity was still in memory and is resumed and the onCreate, onStart, and onResume events will be raised if the activity was recycled and needs to be recreated. So, if you need to display the data again in all cases, you should restore the UI during the onResume.

If it is not necessary to save the data permanently and you only want to save the state of the UI, you can use the onSaveInstanceState event to store the state in a Bundle. This should not be relied upon to save data since the event is not part of the activity lifecycle and is only triggered by the UI  when the activity needs to be recreated or is sent to the background, but not when it is destroyed permanently : it is meant for storing transient view states. Some of the data is already saved by the Android SDK, but you may need to save extra information, for example if you have custom controls. When the user navigates back to the activity and the state of the UI needs to be restored, the bundle containing the state information is accessible from the onRestoreInstanceState event raised if the activity was still in memory, or from the onCreate event raised if the activity was recycled and needs to be recreated.

Related Whitepaper:

Rapid Android Development: Build Rich, Sensor-Based Applications with Processing

Create mobile apps for Android phones and tablets faster and more easily than you ever imagined

Use 'Processing', the free, award-winning, graphics-savvy language and development environment, to work with the touchscreens, hardware sensors, cameras, network transceivers, and other devices and software in the latest Android phones and tablets.

Get it Now!  

Leave a Reply


five + = 13



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books