I am using Hilt as DI in my Android Studio project.
ServiceTranslate
is a Service
class that has a longer lifecycle than the app itself. The ServiceTranslate
will invoke the TranslateIncompleted
class to perform tasks in the background.
1: Currently, I am using @ApplicationContext private val appContext: Context
in the TranslateIncompleted
class. I believe appContext
is an instance of the UIApp
class, is that correct?
2: Will the app crash when the app is destroyed but ServiceTranslate
continues to run in the background?
class TranslateIncompleted @Inject constructor(
@ApplicationContext private val appContext: Context //I'm afriad that it maybe cause trouble.
): ITranslateIncompleted {
override suspend fun translateIncompletedAndUpdate() {
val myString = appContext.getString(R.string.translate_incompleted)
}
}
@AndroidEntryPoint
class ServiceTranslate: Service() {
@Inject lateinit var translateIncompleted: ITranslateIncompleted
inner class MyBinder : Binder() {
val serviceTranslate: ServiceTranslate
get() = this@ServiceTranslate
}
override fun onBind(intent: Intent): IBinder {
return MyBinder()
}
}
@HiltAndroidApp
class UIApp : Application() {
}