0

I need to save a pressed button id the first time the app is executed. I have an Activity with two buttons that take you to one new activity (there are 2 profiles,like user and supervisor). I would like to save button id the first time that my app is running on the device, so in next executions you don't need to select the profile again. Is that possible?

This is my SelectionActivity code:

class SeleccionarPerfilActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_seleccionar_perfil)
    
    val btnHijo: Button = findViewById(R.id.botonhijo)
    val btnTutor: Button = findViewById(R.id.botonmadre)
    
    btnHijo.setOnClickListener{onClick(btnHijo.id}
    btnTutor.setOnClickListener{onClick(btnTutor.id}
    
}

fun onClick(id:Int) {
    when(id){
     R.id.btnHijo-> {
            val intent=Intent(this,MenuHijoActivity::class.java)
            startActivity(intent)
        }
     R.id.btnTutor-> {
            val intent=Intent(this,MenuTutorActivity::class.java)
            startActivity(intent)
        }

}

}

  • Does this answer your question? [How to use SharedPreferences in Android to store, fetch and edit values](https://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – Adinia May 30 '23 at 08:22

1 Answers1

0

Firstly, store your id with onClick method using by SharedPreferences. After that, retrieve that id in onCreate method and check if that id is null. If it's null, display your buttons, if not call your onClick method by your retrieved id.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 30 '23 at 13:28