【问题标题】:Starting a intent programmatically in Kotlin在 Kotlin 中以编程方式启动意图
【发布时间】:2018-09-17 09:13:48
【问题描述】:

我正在尝试根据用户的电子邮件是否经过验证以及他们是否有用户名来将用户发送到一个活动。到目前为止,我的代码如下所示:

if (isEmailVerified == true) {                                                    
    if (user.displayName == null) {                                                        
        startIntent(NewUserLayoutProfile)
        } else {
         startIntent(MainActivity)
    }
}
if (isEmailVerified == false) {                                                    
startIntent(ResendEmailVerification)
}

private fun startIntent (theIntent: Activity) {

            val i = Intent(this, theIntent::class.java)
            startActivity(i)
    }

如何通过活动?我尝试将它作为字符串传递,但没有奏效。我在这里做错了什么?

【问题讨论】:

  • 你要像MainActivity::class.java一样把::class.java传递给函数,参数可以是theIntent: Class<*>
  • 请将此添加为答案,以便我将答案归功于您。
  • 不,这个问题是将意图发送给一个方法。此外,它还在意图中添加了.putExtra。完全不是一回事。

标签: android android-intent kotlin


【解决方案1】:

这是在 Kotlin 中工作的正确代码:

if (userInfo?.isEmailVerified == false) {
                                        startIntent(ResendEmailVerification::class.java)
                                    } else {
                                        if (userInfo?.displayName != null) {
                                            startIntent(MainActivity::class.java)
                                        } else {
                                            startIntent(NewUserLayoutProfile::class.java)
                                        }
                                    }

private fun startIntent (theIntent: Class<*>) {

            val i = Intent(this, theIntent)
            startActivity(i)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 2011-06-08
    相关资源
    最近更新 更多