首页 > 解决方案 > 有没有办法通过 ViewModel 使用 Live Data 启动活动电子邮件?

问题描述

我知道您不能将活动或片段作为参数传递给视图模型,并且为了通过点击事件启动事件或更新 UI,最好使用 LiveData。但是,我不知道如何使用 LiveData 启动电子邮件活动。就我而言,您无法在类视图模型上启动活动。这是我拥有的代码。(评论中的行只是一个例子,我知道他们不会为此工作)。

MainActivity.kt    
 
val obvserver = Observer<String> {studentEmail.setOnClickListener{ intent = Intent(Intent.ACTION_SEND)
                intent.data = Uri.parse("mailto:")
                intent.type = "message/rfc822"
                intent.putExtra(Intent.EXTRA_EMAIL, selectedStudent.email)
                startActivity(Intent.createChooser(intent, "Send Email"))}}


studentEmail.setOnClickListener {
                //viewModel.
            }

ViewModel.kt
val studentEmail : MutableLiveData<String> by lazy {
MutableLiveData<String>()}

标签: emailkotlinviewmodelandroid-livedata

解决方案


推荐阅读