There are a number of mechanisms in the Android SDK that can be used to hold data, and it is often confusing to decide which one to use and when to trigger the operation. The first and easiest thing you want to do when creating a personal application is to keep some of the data that the user has entered that explicitly needs to be saved. Here are some common ways to persist storage: (with an introduction to Android App development tutorial )
The most basic way to save data is to save it to a file on an external storage device. This kind of storage can be obtained by users and all applications, and can be seen when the device is hung as an external storage. Even if your app uninstalls the file, it will remain until you get the folder where you saved the file by using the Getexternalfiledir method. This is best for files that have been edited by the user, even if the app is uninstalled and it's best to keep the samples and documents.
You can also save files as internal storage, which is private to your application. In this case, the user and other applications get the files and the files are deleted when you apply the uninstallation. This is best for saving some simple data and avoiding the overhead of processing the database.
Alternatively, you can save the user's configuration with the bonding/value pair and use the preference API to create the Sharedpreferences file. You can create a preferenceactivity or an activity save parameter configuration that contains preferencefragment, and their layouts have preferences objects in place of the View object.
Finally, if the data structure you want to save is complex, each application has its own private sqllite database to hold the data. If your app is uninstalled, the data will be destroyed as well.
In addition, there are situations where the application's current state needs to be saved because an event is triggered. For example:
When the device is rotated, the current activity is recreated and the layout is adjusted.
When your app is running in the background by users or other apps, such as a phone call. In addition, the user opens another activity for the application, and the current activity is also placed in the background. Any activity that runs in the background can be recycled when it is out of memory and re-created when needed.When the user explicitly closes the app.
In this case, you need to persist the data entered by the user. When activity is placed in the background or destroyed, the OnPause method is always called, followed by OnStop even if the UI is completely hidden. So it's best to save the data in the OnPause method. Subsequently, when the user contacts the previous activity again, if it is still in memory Onrestart,onstart and the Onresume method will be called, if it has been recycled it needs to be re-common, then OnCreate, OnStart, and Onresume will be called. So if you want to show the previous data again in all cases, you should restore the UI in the Onresume method.
If you do not need to save the data permanently and just want to save the state of the UI, you can save the data in the bundle in the Onsaveinstancestate method. This method cannot be used to hold data because it is not part of the activity life cycle, and is not invoked when the activity needs to be re-created or placed in the background by UI events and destroyed permanently. So it's only used to save some temporary view states. Some of the data has been saved by the Android SDK, but you may need to save some extra information, for example, if you have some custom controls, the UI state needs to be restored when the user presses the Back button to return to the activity, and if the activity is still in memory, The bundle object that holds the state information can be obtained in the Onrestoreinstancestate method, and the bundle object can be obtained in the OnCreate method if the activity is recycled and needs to be recreated.
more relevant content, in the e-mentor Network .
Some common ways to persist storage in Android