How to retrieve the state of a button with SharedPreferences in Android / Kotlin on multiple screens?

0

Friends, I have two activities, the first one I have a sound button on and off and the second I have some buttons like for example a button of name A and when clicked it should know what the sound button is in the first activity ... if in the first activity the power button is in the on state the button A must make the sound or the reverse when in the off state. It turns out a few days ago I have tried and still can not solve. What do you say? I'm waiting for some position.

First screen

This method is in the first screen where I saved the preferences, or at least I thought it saved ...

[

openfuntoChangeButtonVolume(){volume_play_menu.setOnClickListener({v->v.isSelected=!v.isSelectedif(v.isSelected){v.setBackgroundResource(R.drawable.volume_ligado)valprefs=getSharedPreferences("preferencias", Context.MODE_PRIVATE)

            val ed = prefs.edit()

            ed.putInt("chave", 1)

            ed.commit()
        }

        else {

            v.setBackgroundResource(R.drawable.volume_mudo)
        }
    })

Second screen This method stays on the second screen when I try to retrieve the preferences created on the first screen. The abc_aprender_a button should emit a sound depending on the state of the button created in the first activity.

fun goFirstLetter () {

    abc_aprender_a.setOnClickListener({

        v ->
        v.setBackgroundResource(R.drawable.check)

        val prefs = getSharedPreferences("preferencias", Context.MODE_PRIVATE)
        //Imagino que aqui eu consiga recuperar a chave na primeira tela ou deveria pelo menos 

        val boo = prefs.getInt("chave", 1)
         playSoundInButton()

    
asked by anonymous 30.11.2016 / 12:05

1 answer

0
if (v.isSelected) {
   v.setBackgroundResource(R.drawable.volume_ligado)
} else {
   v.setBackgroundResource(R.drawable.volume_mudo)
}

val prefs = getSharedPreferences("preferencias", Context.MODE_PRIVATE)
val ed = prefs.edit()
ed.putBoolean("chave", v.isSelected)
ed.commit()

abc_aprender_a.setOnClickListener({  v ->
   v.setBackgroundResource(R.drawable.check)
   val prefs = getSharedPreferences("preferencias", Context.MODE_PRIVATE)
   val boo = prefs.getBoolean("chave", true)
   if(boo){
       playSoundInButton()
   }
}
    
01.12.2016 / 20:27