Explain Hilt scoping: @Singleton vs @ActivityRetainedScoped

Tests Hilt component lifecycles. Answer: @Singleton lives for the app process; @ActivityRetainedScoped survives config changes via ActivityRetainedComponent but dies when the activity finishes.
WHAT THIS TESTS: This question tests whether you understand Hilt component hierarchies and how dependency lifecycles map to Android lifecycle owners. The interviewer wants to see that you know which component owns which scope and what happens across configuration changes.
A GOOD ANSWER COVERS: Four things in order. First, define that Hilt creates components that mirror Android classes like Application and Activity. Second, explain that @Singleton is provided by SingletonComponent which lives for the entire application process, so the same instance is shared everywhere in the app. Third, explain that @ActivityRetainedScoped is provided by ActivityRetainedComponent which survives configuration changes because Hilt retains it behind a ViewModel-like holder, but it is destroyed when the user finishes the activity. Fourth, contrast them by noting that Singleton is global while ActivityRetainedScoped is local to one activity instance and dies with it.
COMMON WRONG ANSWERS: Three red flags. First, saying both scopes are the same because they both outlive a single Activity instance. Second, claiming that @ActivityRetainedScoped is shared across multiple activities; it is not, each activity gets its own ActivityRetainedComponent. Third, stating that retained scoped objects survive process death; they do not, only saved state handles and persistent storage survive process death.
LIKELY FOLLOW-UPS: The interviewer may ask when to choose one over the other. They may ask how @ActivityScoped differs from @ActivityRetainedScoped. They may ask how to share state between activities or whether you can inject a @Singleton into an @ActivityRetainedScoped dependency.
ONE CONCRETE EXAMPLE: Imagine a UserRepository backed by an in-memory cache. If you scope it @Singleton, the cache lives for the whole app and is shared across all screens. If you scope it @ActivityRetainedScoped, the cache survives rotation but is recreated when the user navigates to a new activity and finishes the current one. If the cache holds large bitmaps, retained scope avoids reloading on rotation without leaking memory across the whole app.
Read the original → developer.android.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.