如何使用Hilt将参数从活动传递到ViewModel(没有ViewModel工厂)
在我的活动中,我从意图附加器中启动了多个变量。到目前为止,我正在使用ViewModeFactory将这些变量作为参数传递给我的ViewModel。
我如何消除对剑术的ViewModelFacotory的需求
,这是我的活动类中的两个变量
class CommentsActivity : AppCompatActivity() {
private lateinit var viewModel: CommentsViewModel
override fun onCreate(savedInstanceState: Bundle?) {
val contentId = intent.getStringExtra(CONTENT_ID_FIELD) //nullable strings
val highlightedCommentId = intent.getStringExtra(HIGHLIGHTED_COMMENT_ID_RF) //nullable strings
val commentsViewModelFactory = CommentsViewModelFactory(
contentId,
highlightedCommentId
)
viewModel = ViewModelProvider(this, commentsViewModelFactory[CommentsViewModel::class.java]
}
}
,这是我的ViewModel
class CommentsViewMode(
contentId : String?,
highlightedCo;mmentId : String?,
) : ViewModel() {
//logic code here
}
我的应用程序已经设置为使用刀具,但是在这种情况下,我如何通过这两个变量并完全消除ViewModeFactory
In my activity, I have multiple variables being initiated from Intent Extras. As of now I am using ViewModelFactory to pass these variables as arguments to my viewModel.
How do I eliminate the need for ViewModelFacotory with hilt
Here are two variables in my Activity class
class CommentsActivity : AppCompatActivity() {
private lateinit var viewModel: CommentsViewModel
override fun onCreate(savedInstanceState: Bundle?) {
val contentId = intent.getStringExtra(CONTENT_ID_FIELD) //nullable strings
val highlightedCommentId = intent.getStringExtra(HIGHLIGHTED_COMMENT_ID_RF) //nullable strings
val commentsViewModelFactory = CommentsViewModelFactory(
contentId,
highlightedCommentId
)
viewModel = ViewModelProvider(this, commentsViewModelFactory[CommentsViewModel::class.java]
}
}
Here is my viewModel
class CommentsViewMode(
contentId : String?,
highlightedCo;mmentId : String?,
) : ViewModel() {
//logic code here
}
My app is already set up to use hilt but in this case How can I pass these 2 variables and eliminate the viewModelFactory entirely
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
诀窍是仅初始化这些变量一次,而活动可以多次创建活动。在我的应用程序中,我使用一个标志。
查看模型:
另外,您应该知道,匕首项目中有一个空旷的问题,完全出于此功能:
https://github.com/google/google/dagger/dagger/dagger/issues/2287 欢迎遵循进度。
The trick is to initialize those variables only once, while the activity can be created multiple times. In my apps, I use a flag.
View model:
Also, you should know that there is an open issue in dagger project exactly for this capability:
https://github.com/google/dagger/issues/2287
You're welcome to follow the progress.
如果您想有效使用刀柄,您可以按照此步骤操作
在视图模型中使用@HiltViewModel,
您也不再需要任何ViewModeFactory!一切都为您完成!在您的活动或片段中,您现在只能直接使用KTX ViewModels()。
或者,如果您想将基本类用于片段和活动,则可以使用此代码通过ViewModel类
If you want to use hilt effectively u can follow this steps
Use @HiltViewModel in your view model
Also you no longer need any ViewModelFactory! All is done for you! In your activity or fragment, you can now just use KTX viewModels() directly.
Or if you want to use base classes for fragment and activity you can use this code to pass viewModel class