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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


− five = 4



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
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.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close