{"id":133,"date":"2014-05-06T07:04:38","date_gmt":"2014-05-06T11:04:38","guid":{"rendered":"https:\/\/cindypotvin.com\/?p=133"},"modified":"2017-02-18T16:35:54","modified_gmt":"2017-02-18T21:35:54","slug":"introduction-how-to-save-data-in-your-android-application","status":"publish","type":"post","link":"https:\/\/cindypotvin.com\/introduction-how-to-save-data-in-your-android-application\/","title":{"rendered":"Introduction : How to save data in your Android application"},"content":{"rendered":"
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\u2019ll 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 :<\/p>\n
Also, there are many cases where the current state of the application needs to be saved because an event was triggered. For example :<\/p>\n
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<\/em> event is always raised, followed by the onStop<\/em> event if the IU is completely hidden, so saving persistent data is best done during the onPause<\/em> event. Later on, when the user accesses the activity again, the onRestart<\/em>, onStart<\/em> and onResume<\/em> events will be raised if the activity was still in memory and is resumed and the onCreate<\/em>, onStart<\/em>, and onResume<\/em> 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<\/em>.<\/p>\n 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<\/em> event to store the state in a Bundle<\/em>. 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\u00a0 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<\/em> event raised if the activity was still in memory, or from the onCreate<\/em> event raised if the activity was recycled and needs to be recreated.<\/p>\n Here are all the other articles in the series :<\/p>\n Saving data to a file in your Android application<\/a>
\nSaving preferences in your Android application<\/a>
\nSaving to a SQLite database in your Android application<\/a>
\nStoring the state of an activity of your Android application<\/a>