This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object PreferenceHelper { | |
fun defaultPrefs(context: Context): SharedPreferences | |
= PreferenceManager.getDefaultSharedPreferences(context) | |
fun customPrefs(context: Context, name: String): SharedPreferences | |
= context.getSharedPreferences(name, Context.MODE_PRIVATE) | |
inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) { | |
val editor = this.edit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val prefs = defaultPrefs(this) | |
prefs[Consts.SharedPrefs.KEY] = "any_type_of_value" //setter | |
val value: String? = prefs[Consts.SharedPrefs.KEY] //getter | |
val anotherValue: Int? = prefs[Consts.SharedPrefs.KEY, 10] //getter with default value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.experiments.preferencehelper | |
import android.os.Bundle | |
import android.support.v7.app.AppCompatActivity | |
import android.util.Log | |
import com.experiments.preferencehelper.PreferenceHelper.get | |
import com.experiments.preferencehelper.PreferenceHelper.set | |
class MainActivity : AppCompatActivity() { |
OlderNewer