Tag: sharedpreferences

以共享首选项存储和检索类对象

在Android中,我们可以将共享首选项中的类的对象存储起来,然后再检索对象? 如果有可能怎么做呢? 如果不可能的话,还有其他的可能性吗? 我知道序列化是一种选择,但我正在寻找使用共享偏好的可能性。

Android – SharedPreferences – 上下文

我想在android中使用kotlin为我的SharedPreference创建一个辅助类。 不幸的是,我需要Context ,我不希望每次调用一个首选项时将其设置为参数。 如果我使用上下文的伴侣对象,并将其设置在应用程序启动,我得到以下错误: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run) Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run) 那么如何获得上下文,而不是每次调用偏好时都通过它? var isWorking: Boolean get() = getBoolean(IS_WORKING) set(isWorking) = setPreference(IS_WORKING, isWorking) private fun […]

Kotlin – 不能使用'T'作为实体类型参数。 改用班级

我有一个简单的帮助函数来从SharedPreferences中获取值,如下所示: operator inline fun <reified T : Any> SharedPreferences.get(key: String, defaultValue: T? = null): T? { return when (T::class) { String::class -> getString(key, defaultValue as? String) as T? Int::class -> getInt(key, defaultValue as? Int ?: -1) as T? Boolean::class -> getBoolean(key, defaultValue as? Boolean ?: false) as T? Float::class -> getFloat(key, defaultValue as? Float ?: […]