首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >匕首高精度注入ActivityContext

匕首高精度注入ActivityContext
EN

Stack Overflow用户
提问于 2021-04-04 20:32:58
回答 1查看 3.1K关注 0票数 6

我正在用Dagger-Hilt注入一个在ViewModel中依赖于@ActivityContext的类,这个模块安装在ActivityComponent中,作用域为活动,每当我试图编译时,它都会抛出一个错误。关于您的信息,我还有ActivityRetainedComponent和SingletonComponent injecting @ApplicationContext的其他模块。

现在我想弄清楚这个错误意味着什么。

代码语言:javascript
运行
复制
error: [Dagger/MissingBinding] com.rober.fileshortcut_whereismyfile.utils.PermissionsUtils cannot be provided without an @Inject constructor or an @Provides-annotated method.
  public abstract static class SingletonC implements App_GeneratedInjector,
                         ^
      com.rober.fileshortcut_whereismyfile.utils.PermissionsUtils is injected at
          com.rober.fileshortcut_whereismyfile.ui.fragments.filefragment.FileViewModel(�, permissionsUtils, �)
      com.rober.fileshortcut_whereismyfile.ui.fragments.filefragment.FileViewModel is injected at
          com.rober.fileshortcut_whereismyfile.ui.fragments.filefragment.FileViewModel_HiltModules.BindsModule.binds(vm)
      @dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
          dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.rober.fileshortcut_whereismyfile.App_HiltComponents.SingletonC ? com.rober.fileshortcut_whereismyfile.App_HiltComponents.ActivityRetainedC ? com.rober.fileshortcut_whereismyfile.App_HiltComponents.ViewModelC]

这是代码(我不认为这里有什么问题)

代码语言:javascript
运行
复制
@Module
@InstallIn(ActivityComponent::class)
object PermissionModule {

    @ExperimentalCoroutinesApi
    @Provides
    @ActivityScoped
    fun providePermissionsHelper(
        @ActivityContext context: Context,
    ) = PermissionsUtils(context)
}
代码语言:javascript
运行
复制
@HiltViewModel
@ExperimentalCoroutinesApi
class FileViewModel @Inject constructor(
    private val class1: Class1,
    private val class2: Class2,
    private val class3: Class3,
    private val permissionsUtils: PermissionsUtils //Here's the one I'm injecting
)

我的猜测:它告诉我我不能注入ActivityComponent,因为FileViewModel是在ActivityComponent中注入的,因为这个组件中安装了提供给视图模型的其他依赖项。

问题:到底是怎么回事?我遗漏了什么,是否有任何使用ActivityComponent的解决方案?我真的需要那个班的@ActivityContext

SingletonComponent/ViewModelComponent/ActivityRetainedComponent,编辑:编辑:当我切换到@InstallIn(ActivityRetainedComponent::class)并将上下文转换为@ApplicationContext context: Context时,这是可行的,请注意,它与一起工作是有意义的。但我还是不知道如何让它在@ActivityComponent上工作

编辑2:我得出的结论是不可能的,因为您正在ViewModel中安装,与ViewModel一起工作的组件是 Singleton/ActivityRetained/ViewModel,所以无法在ViewModel中注入ActivityContext,因为它需要组件@ActivityComponent,这比ViewModelComponent低1级。

资料来源: https://dagger.dev/hilt/components#fn:1

EN

Stack Overflow用户

回答已采纳

发布于 2021-04-24 21:38:58

因此无法在ViewModel中注入ActivityContext,因为它需要组件@ActivityComponent,这比ViewModelComponent低1级。

你说对了。

从生命周期的角度考虑Dagger/Hilt组件(和作用域)。在安卓系统中,ViewModel的生命周期要比包含ActivityFragment的生命周期长。这就是全班的重点,不幸的是这个名字。把它们看作是RetainedInstanceAnObjectThatSurvivesActivityConfigurationChanges可能会有帮助。是啊,这些不像ViewModel那么吸引人。

如果将一个短期对象( Activity)注入长寿对象( ViewModel),则后者将保留对前者的引用。这是内存泄漏-- Activity及其包含的所有内容(例如视图、位图)都保留在内存中,即使不再使用它。其中的几个,你冒着OutOfMemoryError的风险。

然而,反过来--即将一个长寿的物体注入一个短命的物体--是安全的。这就是为什么您让它使用Singleton/ActivityRetained/ViewModel组件。

Hilt的组件层次结构使您更难创建内存泄漏。所有这些都发生在编译时,与基于运行时的解决方案相比,这给了您更快的反馈。比你的用户发现的崩溃要好得多!

票数 13
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66945616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档