Last active
December 18, 2020 10:12
-
-
Save albaspazio/14c999ff9c428afa2370173b747ef4b9 to your computer and use it in GitHub Desktop.
Singleton solution to manage SharedPreferences
This file contains hidden or 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
import android.content.Context | |
import androidx.preference.PreferenceManager | |
// concrete Singleton | |
object MyPrefManager: SharedPreferencePrimitives() { | |
// here specify preferences' names | |
private const val attachment = "attachment" | |
//call it once | |
fun init(context:Context, pref_name:String="", mode:Int = Context.MODE_PRIVATE){ | |
if(isInitialized()) return // prevent multiple init | |
pref = if(pref_name.isEmpty()) PreferenceManager.getDefaultSharedPreferences(context) | |
else context.getSharedPreferences(pref_name, mode) | |
editor = pref.edit() | |
} | |
fun setAttachment(useattach: Boolean) { | |
attachment.put(useattach) | |
} | |
fun getAttachment() = attachment.getBoolean() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment