How to start new activity on button click

后端 未结 24 1710
傲寒
傲寒 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:41

    // In Kotlin , you can do as /* In First Activity, let in activity layout there is button which has id as button. Suppose I have to pass data as String type from one activity to another */

         val btn = findViewById

    // In Second Activity, you can get data from another activity as

     val name = intent.getStringExtra("KEY")
    

    /* Suppose you have to pass a Custom Object then it should be Parcelable. let there is class Collage type which I have to pass from one activity to another */

    import android.os.Parcelable
    import kotlinx.android.parcel.Parcelize
    
    @Parcelize
    class Collage(val name: String, val mobile: String, val email: String) : Parcelable
    

    /* Activity First , let here data is Collage type. which I have to pass to another activity. */

    val btn = findViewById

    // then from second Activity we will get as

    val item = intent.extras?.getParcelable("KEY")
    

提交回复
热议问题