This can be done in two ways. One is storing them in a global variables and second is storing the data in shared preferences. The problem with storing data in global variable is data will be lost once user closes the application, but storing the data in shared preferences will be persistent even though user closes the application. Application shared preferences allows you to save and retrieve key, value pair data. Before getting into tutorial, I am giving basic information needed to work with shared preferences.
|Published (Last):||6 October 2012|
|PDF File Size:||14.11 Mb|
|ePub File Size:||12.29 Mb|
|Price:||Free* [*Free Regsitration Required]|
Typically it is used to cache user local data such as login forms. The data is stored in the form of a key-value pair.
You can name multiple files of data to hold the SharedPreferences. Here String is the name of the SharedPreferences file and int is the Context passed. Editor is used to edit values in the SharedPreferences. The above way to save and retrieve values from a SharedPreference is nearly the same as we do in Java. Context import android.
SharedPreferences import android. AppCompatActivity import android. Bundle import android. PreferenceManager import android. View import com. In the above code, we create a singleton class using the object syntax. Inside it, we declare an inline higher order function named editMe that does the stuff for the edit operation. We use the get and set Kotlin properties to retrieve and set the data in the shared preferences. This makes the code a lot cleaner. Furthermore, we can also make the code more concise by using another Kotlin higher order function shown below.
The output of the above application in action is given below. This brings an end to this tutorial. You can download the source code from the link below.
Save key-value data
This tutorial explains, how to use Sharedpreferences to store data internally in an Android Application. Introduction Android provides many ways to store the data internally in an Android Application. One of the way is SharedPreferences. SharedPreferences allow you to store data using Key value pair. This tutorial explains how to store the data list internally using SharedPreferences.
Die Anleitung zu Android SharedPreferences
Android SharedPreferences Using Kotlin