Home > Back-end >  Android - How can I save user setting preferences even after the user closes the app with KOTLIN?
Android - How can I save user setting preferences even after the user closes the app with KOTLIN?

Time:09-24

My situation is:

I have my settings fragment. Inside my settings fragment I can change language and theme. The question is: how can i save the user settings preferences even after the user closes the application, and once the user will reopen the app, he will have the options choosed before.

Could you give me some examples? Thanks

CodePudding user response:

get the sharedPreferences of the app in a fragment

mSharedPreferences = requireActivity().getSharedPreferences("createAKey" , Context.MODE_PRIVATE)

how to get a saved sharedPreference, the second parameter is like a else if you dont have saved before

var email = mSharedPreferences.getString("email_key" , "")

this is how you save a sharedPreference

mSharedPreferences.edit {
    val email = "[email protected]"
    putString("email_key" , email)
}

}

  • Related