How to pass a boolean between intents

前端 未结 3 1181
轻奢々
轻奢々 2020-12-06 00:43

I need to pass a boolean value to and intent and back again when the back button is pressed. The goal is to set the boolean and use a conditional to prevent multiple launche

3条回答
  •  無奈伤痛
    2020-12-06 01:03

    This is how you do it in Kotlin :

    val intent = Intent(this@MainActivity, SecondActivity::class.java)
                intent.putExtra("sample", true)
                startActivity(intent)
    
    var sample = false
    sample = intent.getBooleanExtra("sample", sample)
    println(sample)
    

    Output sample = true

提交回复
热议问题