1. 程式人生 > >kotlin Activity跳轉以及資料傳輸

kotlin Activity跳轉以及資料傳輸

 1  在要跳轉到介面定義newInstance 並定義幾個引數名

 companion object {
       
        private const val KEY_TITLE = "title"
        private const val KEY_SUBTITLE = "subtitle"
        private const val KEY_CAR_ID = "car_id"
        fun newInstance(context: Activity, title: String, id: Int,subtitle: String) {
            context.startActivity<CarListDetailActivity>(KEY_TITLE to title, KEY_CAR_ID to id,KEY_SUBTITLE to subtitle)
        }
    }

2  在使用跳轉的介面 傳相關資料進去即可

 CarListDetailActivity.newInstance(this,data.number,data.id,collapsing_toolbar_layout.title.toString())

3 在要跳轉到介面定義 使用kotlin 使用單利接受即可

   private val subtitle: String by lazy { intent.getStringExtra(KEY_SUBTITLE) }
    private val title: String by lazy { intent.getStringExtra(KEY_TITLE) }
    private val car_id: Int by lazy { intent.getIntExtra(KEY_CAR_ID, -1) }