Skip to content

Extension for SharedPreferences

In order to make you use SharedPreferences more convenient, VastTools provides following extension functions.

fun SharedPreferences.string(defaultValue: String? = null): ReadWriteProperty<Any, String>

Currently support data type:

  • String
  • Set< String>
  • Int
  • Long
  • Float
  • Boolean
  • Double

Get started

Version 0.5.6

  • Implement ISharedPreferencesOwner

    object SpExample : ISharedPreferencesOwner {
        override val name: String = "Sp name"
        override val kv: SharedPreferences = ... // SharedPreferences instance
    
        // Declare stored fields
        var isDark by boolean(false)
    }
    
  • Write to shared preferences

    SpExample.isDark = !SpExample.isDark
    
  • Read from shared preferences

    val isDark = SpExample.isDark
    
  • Clear shared preferences

    Calling clearAll() to remove all values ​​from SharedPreferences.

    sp.clearAll()