Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于ViewModel的实例化 #4

Open
Kotlin2022 opened this issue Jul 23, 2024 · 2 comments
Open

关于ViewModel的实例化 #4

Kotlin2022 opened this issue Jul 23, 2024 · 2 comments

Comments

@Kotlin2022
Copy link

我看项目中的ViewModel都是通过by lazy 对象的构造方法实例化的,这样实例化的ViewModel 是不是不能保证ViewModel与Fragment、activity的生命周期关联?例如:
private val mViewModel: WanViewModel by lazy { WanViewModel() }
是不是应该用ViewModelProvider或者ViewModelProvider.Factory去实例化比较好?

@Khaos116
Copy link
Owner

@Kotlin2022 如果你要多页面共用数据,就使用ViewModelProvider共享数据

@Khaos116
Copy link
Owner

我当时转kotlin的时候使用没那么熟练,也是小白,别人这样我就这样了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants