简体   繁体   English

当我必须使用 Hilt 作为依赖注入时,我可以手动创建一个实例吗?

[英]Can I create an instance manually when I have to use Hilt as dependency injection?

I use Hilt as dependency injection in my project.我在我的项目中使用 Hilt 作为依赖项注入。

The ObjectModule module provides the instance of MediaRecorder , so I can use it in both SoundViewModel and HandleMeter . ObjectModule模块提供了MediaRecorder的实例,所以我可以在SoundViewModelHandleMeter中使用它。

In order to release resources, I have to assign null to mRecorder in fun stop() of SoundViewModel , so I need to create an instance of MediaRecorder in fun play() as needed.为了释放资源,我必须在nullfun stop()中将mRecorder分配给SoundViewModel ,因此我需要根据需要在fun play()中创建一个MediaRecorder实例。

1: Do I need create the instance using mRecorder = MediaRecorder() by myself? 1:需要自己用mRecorder = MediaRecorder()创建实例吗? Can I get it from dependency injection?我可以从依赖注入中获取它吗?

2: Will the instance of MediaRecorder in class HandleMeter @Inject constructor(val mRecorderFromDI: MediaRecorder) be recreated by dependency injection again when I have created it using mRecorder = MediaRecorder() in fun play() ? 2:当我在fun play()中使用mRecorder = MediaRecorder()创建它时,是否会再次通过依赖注入重新创建class HandleMeter @Inject constructor(val mRecorderFromDI: MediaRecorder)中的MediaRecorder实例?

@HiltViewModel
class SoundViewModel @Inject constructor(
    private val aHandleMeter: HandleMeter,
    private var mRecorder: MediaRecorder?
 ): ViewModel() {

    fun play() {
        //In order to handel re-play, so I write the following code.
        if (mRecorder==null) {
            mRecorder =  MediaRecorder()  //Do I need create by myself? Can I get it from dependency injection ?
        }
        
        mRecorder?.let {
            with(it) {
                setAudioSource(MediaRecorder.AudioSource.MIC);
                 ...
                prepare()
                start()
            }
        }

        val my= aHandleMeter.getMSoundDensity() 
        ...
    }
    
    fun stop() {
        try {          
            mRecorder?.stop()    
            mRecorder?.release()
            mRecorder = null
        } catch (e: Exception) {
        }
    }  
 
}


@InstallIn(SingletonComponent::class)
@Module
object ObjectModule {
    @Provides
    @Singleton
    fun provideMediaRecorder(): MediaRecorder {
        return  MediaRecorder()
    }
}


class HandleMeter @Inject constructor(val mRecorderFromDI: MediaRecorder): IHandleMeter {

    override fun getMSoundDensity(): Flow<MSoundDensity> {
      val temp = mRecorderFromDI.maxAmplitude
       ...
    }
}
  1. You can create the instance manually and you can also get that from your Dependency injection.您可以手动创建实例,也可以从依赖注入中获取实例。 There is no difference as both will return a new instance.没有区别,因为两者都会返回一个新实例。

  2. No, the instance is not recreated by Dependency injection when you do mRecorder = MediaRecorder() .不,当您执行mRecorder = MediaRecorder()时,依赖注入不会重新创建实例。 You actually lose reference to initial instance created by dependency injection and get a new instance of MediaRecorder() assigned to mRecorder .您实际上失去了对依赖注入创建的初始实例的引用,并获得了分配给mRecorderMediaRecorder()的新实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Hilt 依赖注入时无法创建 ViewModel 实例 - Cannot create ViewModel instance when using Hilt dependency injection 如何在 Android Studio 中使用 Hilt 依赖注入 MediaRecorder? - How can I dependency injection MediaRecorder with Hilt in Android Studio? 我如何在 Android Studio 中使用 Hilt for ViewModel() 依赖注入上下文? - How can I dependency injection Context with Hilt for ViewModel() in Android Studio? 为什么我仍然需要使用带有依赖注入库 (Hilt) 的 singleton 类? - Why do I still need to use singleton classes with a dependency injection library (Hilt)? 如何在 Android Studio 中使用 Hilt 将依赖项注入上下文到 ViewModel? - How can I dependency injection Context into ViewModel using Hilt in Android Studio? 使用 hilt-android 在 Singleton 实例进行依赖注入 - Dependency injection at Singleton instance with hilt-android 当我只对接口的一种实现使用依赖注入时,是否需要创建@Module? - Do I need to create @Module when I use dependency injection for only one implementation of an interface? 如何将片段注入活动,以便我不必手动创建其实例? - How can I inject fragment into activity so that I don't have to manually create its instance? HILT 依赖注入 - HILT dependency injection 将 Hilt 与 Compose 一起使用时无法创建 ViewModel 的实例 - Can't create an instance of ViewModel when using Hilt with Compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM