为什么我应该为视图模型使用视图模型提供者?
Why should I use viewmodelproviders for viewmodels?
为什么我应该为视图模型使用视图模型提供程序?
为什么我不能将自定义单例注释添加到我的视图模型,然后将此视图模型注入片段 class?
像这样:
@MainScope
class MainViewModel @Inject constructor(): ViewModel()
然后:
open class BaseFragment<T: ViewModel>: DaggerFragment() {
@Inject
protected lateinit var viewModel: T
两种情况都与屏幕旋转无关。
单例注解有什么缺点吗?
我只看到优点,使用这种方法我不需要 copy/paste 大量代码。
Why should I use viewmodelproviders for viewmodels?
让 viewModel.onCleared()
回调在正确的时间被 ComponentActivity
正确调用。
(并确保它只为给定的 ViewModelStoreOwner 创建一次)。
Why I just can't add custom singleton annotation to my viewmodel, and then inject this viewmodel to fragment class?
因为 ComponentActivity
无法在正确的时间正确调用 viewModel.onCleared()
回调。
Is there any drawbacks of singleton annotation case? I see only advantages,
你没有得到 viewModel.onCleared()
。
此外,如果您有一个单例变体,那么 ViewModel 将不会与其封闭的完成一起消失 Activity,并且即使在向后导航(这可能不是预期的)时也能保持活力。
with this approach I don't need to copy/paste tons of code.
您正在使用 Kotlin。使用扩展函数。
为什么我应该为视图模型使用视图模型提供程序?
为什么我不能将自定义单例注释添加到我的视图模型,然后将此视图模型注入片段 class?
像这样:
@MainScope
class MainViewModel @Inject constructor(): ViewModel()
然后:
open class BaseFragment<T: ViewModel>: DaggerFragment() {
@Inject
protected lateinit var viewModel: T
两种情况都与屏幕旋转无关。
单例注解有什么缺点吗? 我只看到优点,使用这种方法我不需要 copy/paste 大量代码。
Why should I use viewmodelproviders for viewmodels?
让 viewModel.onCleared()
回调在正确的时间被 ComponentActivity
正确调用。
(并确保它只为给定的 ViewModelStoreOwner 创建一次)。
Why I just can't add custom singleton annotation to my viewmodel, and then inject this viewmodel to fragment class?
因为 ComponentActivity
无法在正确的时间正确调用 viewModel.onCleared()
回调。
Is there any drawbacks of singleton annotation case? I see only advantages,
你没有得到 viewModel.onCleared()
。
此外,如果您有一个单例变体,那么 ViewModel 将不会与其封闭的完成一起消失 Activity,并且即使在向后导航(这可能不是预期的)时也能保持活力。
with this approach I don't need to copy/paste tons of code.
您正在使用 Kotlin。使用扩展函数。