簡體   English   中英

kotlin / java sharedPreferences在android studio中的含義

[英]kotlin / java sharedPreferences meaning in android studio

在 Kotlin / android studio 中,內置函數 sharedPreferences 到底是什么意思? 我已嘗試閱讀官方 android studio 文檔中的文檔,並希望簡單了解如何使用它來共享數據。

Kotlin/Java 中的 SharedPreferences 可用於在系統內存中存儲類型(String、Int、Long、Float、Boolean 等)的數據值,以便即使在應用程序終止后您也可以稍后檢索它。

假設您要保存一個字符串值,

// Declare a shared preference
val sharedPref = getSharedPreferences('MySharedPref', MODE_PRIVATE)

// Initial value
val doorState: String = "Opened"

editDoorState(doorState)
// So whenever the doorState changes, we can save it, so even if the application is
// terminated or killed and restarts again, the last edited value can be retrieved.
// This is commonly used to store a single data value.



// Function to save the doorstate change(of type String) 
fun editDoorState(value: String) {
  val sharedPrefEditor = sharedPref.edit()

  // Where the 'doorState' is the SharedPreference Key.
  // To retrieve the value, key must be produced.
  sharedPrefEditor.putString('doorState', value)
  sharedPrefEditor.commit()
}

有關更多信息,請在此處查看 Android 的官方文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM