Kotlin Android start new Activity

后端 未结 17 2303
不知归路
不知归路 2020-12-07 23:33

I want to start another activity on Android but I get this error:

Please specify constructor invocation; classifier \'Page2\' does not have a companio

17条回答
  •  清歌不尽
    2020-12-08 00:30

    You can generally simplify the specification of the parameter BlahActivity::class.java by defining an inline reified generic function.

    inline fun  Context.createIntent() =
        Intent(this, T::class.java)
    

    Because that lets you do

    startActivity(createIntent()) 
    

    Or even simpler

    inline fun  Activity.startActivity() {
        startActivity(createIntent()) 
    } 
    

    So it's now

    startActivity() 
    

提交回复
热议问题