[TIL] ๐ŸŒผ24/04/28๐ŸŒผ#Hilt Components

0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
90/100
post-thumbnail

[TIL] ๐ŸŒผ24/04/28๐ŸŒผ#Hilt Components

Hilt Components

๐Ÿ“Œ์ฐธ๊ณ ์ž๋ฃŒ

  • @InstallIn annotation๋กœ Hilt Component ์ง€์ •
    -> ๊ฐ Component์™€ ๋Œ€์‘๋˜๋Š” Android class์— ์ข…์†์„ฑ ์ฃผ์ž…

    Hilt ComponentInjector for (Android class)
    SingletonComponentApplication
    ActivityRetainedComponentN/A
    ViewModelComponentViewModel
    ActivityComponentActivity
    FragmentComponentFragment
    ViewComponentView
    ViewWithFragmentComponentView annotated with @WithFragmentBindings
    ServiceComponentService
  • Hilt๋Š” ๋Œ€์‘๋˜๋Š” Android class์˜ ์ƒ๋ช…์ฃผ๊ธฐ์— ๋งž์ถฐ Component created/destroyed

    • ex. ActivityComponent: Activity์˜ onCreate()์— created, Activity์˜ onDestroy()์— destroyed
    • ex. ActivityRetainedComponent: Activity์˜ ์ฒซ๋ฒˆ์งธ onCreate()์— created, Activity์˜ ๋งˆ์ง€๋ง‰ onDestroy()์— destroyed
      (ActivityRetainedComponent lives across configuration changes)
  • Hilt์˜ binding์€ ๊ธฐ๋ณธ์ ์œผ๋กœ unscoped
    -> binding ์š”์ฒญ๋  ๋•Œ๋งˆ๋‹ค, ํ•„์š”ํ•œ ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค ์ƒˆ๋กœ ์ƒ์„ฑ

  • Scope annotation์œผ๋กœ scope ์ง€์ • ๊ฐ€๋Šฅ
    -> ํ•ด๋‹น scope ๋‚ด์—์„œ๋Š”, ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ X, ๊ฐ™์€ ์ธ์Šคํ„ด์Šค ๊ณต์œ 

    Android classHilt ComponentScope
    ApplicationSingletonComponent@Singleton
    ActivityActivityRetainedComponent@ActivityRetainedScoped
    ViewModelViewModelComponent@ViewModelScoped
    ActivityActivityComponent@ActivityScoped
    FragmentFragmentComponent@FragmentScoped
    ViewViewComponent@ViewScoped
    View annotated with @WithFragmentBindingsViewWithFragmentComponent@ViewScoped
    ServiceServiceComponent@ServiceScoped
  • Hilt Component์— binding์„ scoping
    -> Component destroyed ๋  ๋•Œ๊นŒ์ง€ binding ๋ฉ”๋ชจ๋ฆฌ์— ์œ ์ง€ํ•ด์•ผ
    -> ๋น„์šฉ ํผ!

  • ๊ธฐ๋ณธ์ ์œผ๋กœ View Component๋Š” Activity Component์˜ binding์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
  • Fragment ๋‚ด์˜ View Component๊ฐ€ Fragment Component์˜ binding์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด,
    @AndroidEntryPoint ์• ๋…ธํ…Œ์ด์…˜๊ณผ ํ•จ๊ป˜ @WithFragmentBindings ์• ๋…ธํ…Œ์ด์…˜ ํ•„์š”

0๊ฐœ์˜ ๋Œ“๊ธ€