skip to Main Content

I have a code where I called sharedPref.edit() and sharedPref.apply() multiple times. How to make convert it to call only once.

if (success) {
                                val data = response.getJSONObject("data")
                                sharedPreferences.edit().putBoolean("isLoggedIn", true).apply()
                                sharedPreferences.edit()
                                    .putString("user_id", data.getString("user_id")).apply()
                                sharedPreferences.edit().putString("name", data.getString("name"))
                                    .apply()
                                sharedPreferences.edit().putString("email", data.getString("email"))
                                    .apply()
                                sharedPreferences.edit()
                                    .putString("mobile_number", data.getString("mobile_number"))
                                    .apply()
                                sharedPreferences.edit()
                                    .putString("address", data.getString("address")).apply()

                                StyleableToast.Builder(this)
                                    .text("Welcome " + data.getString("name"))
                                    .backgroundColor(Color.RED)
                                    .textColor(Color.WHITE).show()

                                userSuccessfullyLoggedIn()
                            }

I want to use the method call only once.

This can be called once, the returned editor instance can be stored in
a variable and re-used.

How to do this ??

2

Answers


  1. These little steps will organize your code.

    You can put it like this:

    val editor =  sharedPreferences.edit()  
    

    Then use it :

    editor.putBoolean("isLoggedIn", true)
    

    And Add others values without ".apply()"

    Then Put at the End:

     editor.apply()
    
    Login or Signup to reply.
  2. you can create your custom Shared Preferences

    class CustomSharedPreferences {
    
        companion object {
    
            private val PREFERENCES_USER_NAME = "preferences_user_name"
            private var sharedPreferences: SharedPreferences? = null
    
            @Volatile private var instance: CustomSharedPreferences? = null
    
            private val lock = Any()
            operator fun invoke(context: Context) : CustomSharedPreferences = instance ?: synchronized(lock){
                instance ?: makeCustomSharedPreferences(context).also {
                    instance = it
                }
            }
    
            private fun makeCustomSharedPreferences(context: Context) : CustomSharedPreferences{
                sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
                return CustomSharedPreferences()
            }
        }
    
        fun saveUser(name: String, email: String){
            sharedPreferences?.edit(commit = true){
                putString(PREFERENCES_USER_NAME, name)
            }
        }
    
        fun getUser() = sharedPreferences?.getString(PREFERENCES_USER_NAME, "")
    
    }
    

    You can save all information to SP in saveUser().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search