How to start new activity on button click

后端 未结 24 1601
傲寒
傲寒 2020-11-21 05:54

In an Android application, how do you start a new activity (GUI) when a button in another activity is clicked, and how do you pass data between these two activities?

24条回答
  •  臣服心动
    2020-11-21 06:36

    Kotlin

    First Activity

    startActivity(Intent(this, SecondActivity::class.java)
      .putExtra("key", "value"))
    

    Second Activity

    val value = getIntent().getStringExtra("key")
    

    Suggestion

    Always put keys in constant file for more managed way.

    companion object {
        val PUT_EXTRA_USER = "user"
    }
    startActivity(Intent(this, SecondActivity::class.java)
      .putExtra(PUT_EXTRA_USER, "value"))
    

提交回复
热议问题