Search code examples
androiddependency-injectioncode-generationdagger

Can dagger make android activity creation slower?


I have property data manager in Activity A and I am instantiating it's value instance in activity A onCreate() through dagger component.

override fun onCreate(savedInstanceState: Bundle?){ 
   datatManager = coreComponent().provideDataManager() 
}

My questions(probably a silly ones) are: 1] Does dagger generate the code and instantiate the object when I call it on onCreate()? or Dagger being compile time, it already have all the classes ready behind the scene on which Data Manager has dependency? and just pas me the reference when I need it? 2] Does this make creating/starting an activity any slower?


Solution

  • Dagger generates code during compilation time, so the code itself is "ready", but that doesn' mean class instances are. Dagger creates instances every time you access them by default, unless you use a scoping mechanism such as @Singleton.

    If an injected instance is heavy (i.e. does a lot in its constructor) then yes, it can negatively affect your activity creation time.