Blog Kotlin

[Android & Kotlin] Save data and check in Android Studio SharedPreferences

You’ll be able to easily use SharedPreferences to save lots of a flag to download data solely on the first launch of the app. Nevertheless, it is suggested to make use of a database when there’s lots of data. It might be used as an setting variable for simple initialization.

Android Studio 3.1.4
API 28
Kotlin 1.2.61

SharedPreferences

There are a number of storage choices for Android, however this SharedPreferences is translated into Japanese as “Shared Preferences”.

Ref: Storage Options | Android Builders

As you will notice later, the entity of SharedPreferences is an xml file. It is a matter of writing and studying xml file to the memory in the appliance, so it appears that evidently there could also be issues with exchanging a considerable amount of data and buying data across processes.
It is straightforward to save lots of data about true / false or not, whether or not or not the initial startup has been simple.

SharedPreferences, Editor, MODE settings

In the writing case

  1. Instantiate SharedPreferences
  2. Name edit () to get SharedPreferences.Editor
  3. Add a worth utilizing a way reminiscent of putString ()
  4. Write a new worth utilizing apply () or commit ()

String code for writing

There are two writing strategies: apply () and commit (). Since apply is asynchronous and commit () is synchronous processing, apply () appears to be advantageous if there isn’t any want to put in writing instantly.
In line with apply (), it is better to make use of this if it is asynchronous, nevertheless it looks like a case-by-case that when you’ve got problems, use commit (). You have to be careful if multiple writes happen in totally different threads.

This example is outlined as DataStore, and in the mode of MODE_PRIVATE, Secret is input and worth is, for example, “abcdefg”. There are other modes MODE_APPEND.

  1. MODE_PRIVATE
    • Read and write solely from this app
  2. MODE_APPEND
  3. MODE_MULTI_PROCESS
    • That is valid when studying data written in Service utilizing Activity
      Sadly it has been deprecated in Android 6.0

Example of saving and studying String, Int, Boolean, Long, Float.
Save:

reading:

Put it in Undertaking and put it together
MainActivity.kt

activity_main.xml

Resource
strings.xml

Affirm with Gadget File Explorer

Check the data saved in Android Studio 3.0
(Notice) I used to call ADM, however earlier than that additionally it is referred to as DDMS.

From Package deal File Explorer, select the package deal identify underneath / data / data /
Under that, there’s an xml named in the Shared_pref folder. (DataStore.xml)

Click to see that it is an xml file.

sharedprefences a02- [Android & Kotlin] Save data and check in Android Studio SharedPreferences

The “test” set in the check is the worth of “input”.

It was like this.

It’s also possible to use the command run-as to seek out out.

Associated article:

References:
Storage Options | Android Developers
SharedPreferences | Android Builders
apply ()