EPAgg
EPAgg

Reputation: 119

How to save switch setting in Android using Kotlin

I have a need to save the position of a switch (actually 6 switches) as part of user preferences in Android using Kotlin.

I have written the code in Java with no problems, but this code needs to be in Kotlin. I was thinking to use shared preferences as in Java, and have successfully produced code to save the state of one switch. However, when I write code to add a second switch, the first switch takes control of the additional switches and their state is saved as the same as the first. Additionally, all subsequent switches will reproduce the same. I have tried the Kotlin.org code converter/translator, but that is producing a bunch of jabber that I have to clean up before I can compile, then find out that the translated code is likely incomplete.

    private fun onSwitch() {

    val sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE)
    val editor = sharedPreferences.edit()


    push_switch1.isChecked = sharedPreferences.getBoolean(PREF_SWITCH, false)
    push_switch1.setOnCheckedChangeListener { _, isChecked ->
        if (isChecked){
            editor.putBoolean(PREF_SWITCH, push_switch1.isChecked)
            editor.putBoolean(PREF_SWITCH, true)
            Toast.makeText(this@MainActivity, "Push Notification ON", Toast.LENGTH_SHORT).show()
        } else {
            editor.putBoolean(PREF_SWITCH, false)
            Toast.makeText(this@MainActivity, "Push Notification Off", Toast.LENGTH_SHORT).show()
        }
        //editor.apply()
    }
    email_switch1.isChecked = sharedPreferences.getBoolean(PREF_SWITCH, false)
    email_switch1.setOnCheckedChangeListener { _, isChecked ->
        if (isChecked){
            editor.putBoolean(PREF_SWITCH, email_switch1.isChecked)
            editor.putBoolean(PREF_SWITCH, true)
            Toast.makeText(this@MainActivity, "Email Notification ON", Toast.LENGTH_SHORT).show()
        }else{
            editor.putBoolean(PREF_SWITCH, false)
            Toast.makeText(this@MainActivity, "Email Notification OFF", Toast.LENGTH_SHORT).show()
        }
        //editor.apply()
    }
    editor.apply()

This is a preferences page and each on/off switch turns on or off a specific user preference. Additionally, the switch state needs to persist to keep the user's settings.

Upvotes: 2

Views: 5020

Answers (1)

arjay bernardino
arjay bernardino

Reputation: 51

Both of your switch push_switch1 and email_switch1 is using the same Preference KEY which is PREF_SWITCH.

You need to add unique Preference KEY for each of your switch. Add PREF_SWITCH_PUSH and PREF_SWITCH_EMAIL preferences. Then try this...

private fun onSwitch() {

    val sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE)
    val editor = sharedPreferences.edit()


    push_switch1.isChecked = sharedPreferences.getBoolean(PREF_SWITCH_PUSH, false)
    push_switch1.setOnCheckedChangeListener { _, isChecked ->
        if (isChecked){
            editor.putBoolean(PREF_SWITCH_PUSH, true)
            Toast.makeText(this@MainActivity, "Push Notification ON", Toast.LENGTH_SHORT).show()
        } else {
            editor.putBoolean(PREF_SWITCH_PUSH, false)
            Toast.makeText(this@MainActivity, "Push Notification Off", Toast.LENGTH_SHORT).show()
        }
        editor.apply()
    }
    email_switch1.isChecked = sharedPreferences.getBoolean(PREF_SWITCH_EMAIL, false)
    email_switch1.setOnCheckedChangeListener { _, isChecked ->
        if (isChecked){
            editor.putBoolean(PREF_SWITCH_EMAIL, true)
            Toast.makeText(this@MainActivity, "Email Notification ON", Toast.LENGTH_SHORT).show()
        }else{
            editor.putBoolean(PREF_SWITCH_EMAIL, false)
            Toast.makeText(this@MainActivity, "Email Notification OFF", Toast.LENGTH_SHORT).show()
        }
        editor.apply()
    }
}

Upvotes: 2

Related Questions