0
0
Android Kotlinmobile~10 mins

Dependency injection with Hilt in depth in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to annotate an Android Application class for Hilt.

Android Kotlin
import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@[1]
class MyApp : Application() {}
Drag options to blanks, or click blank then click option'
AHiltAndroidApp
BInject
CModule
DProvides
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Inject instead of @HiltAndroidApp
Forgetting to annotate the Application class
Using @Module or @Provides on the Application class
2fill in blank
medium

Complete the code to inject a dependency into an Android Activity using Hilt.

Android Kotlin
import android.os.Bundle
import androidx.activity.ComponentActivity
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @Inject lateinit var repository: [1]

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
}
Drag options to blanks, or click blank then click option'
AModule
BInject
CMyRepository
DProvides
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Inject as the type instead of the class name
Not annotating the Activity with @AndroidEntryPoint
Forgetting to declare the variable as lateinit
3fill in blank
hard

Fix the error in the Hilt module by completing the annotation to provide a singleton instance.

Android Kotlin
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    @[1]
    fun provideService(): ApiService {
        return ApiServiceImpl()
    }
}
Drag options to blanks, or click blank then click option'
ASingleton
BInject
CHiltAndroidApp
DAndroidEntryPoint
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Inject instead of @Singleton on provider methods
Forgetting to annotate the module with @InstallIn
Using @AndroidEntryPoint or @HiltAndroidApp on provider methods
4fill in blank
hard

Fill both blanks to create a Hilt module that provides a Retrofit instance.

Android Kotlin
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
    @Singleton
    @Provides
    fun provideRetrofit(): Retrofit {
        return Retrofit.Builder()
            .baseUrl([1])
            .addConverterFactory([2])
            .build()
    }
}
Drag options to blanks, or click blank then click option'
A"https://api.example.com/"
BGsonConverterFactory.create()
CRetrofit.create()
D"http://localhost/"
Attempts:
3 left
💡 Hint
Common Mistakes
Using Retrofit.create() instead of GsonConverterFactory.create()
Forgetting quotes around the base URL string
Using a wrong URL like localhost for production
5fill in blank
hard

Fill all three blanks to create a Hilt ViewModel with an injected repository and a saved state handle.

Android Kotlin
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class UserViewModel @Inject constructor(
    private val repository: [1],
    private val savedStateHandle: [2]
) : ViewModel() {

    fun getUserId(): String? {
        return savedStateHandle.get<String>([3])
    }
}
Drag options to blanks, or click blank then click option'
AUserRepository
BSavedStateHandle
C"userId"
DViewModel
Attempts:
3 left
💡 Hint
Common Mistakes
Using ViewModel as a type for savedStateHandle
Forgetting quotes around the key string
Using wrong repository class name