ViewModel (3) 썸네일형 리스트형 [Android] Fragment.rootFragment extension property val Fragment.rootFragment: Fragment get() = parentFragment?.rootFragment ?: this Fragment가 여러 개 쌓여있는 상황일 때 가장 최상단 Fragment를 반환한다. 재귀방식으로 구현해 봤으며, RootFragment 레벨에서 ViewModel 공유가 필요할 때 사용할 수 있다. class TestFragment: Fragment() { private val testViewModel: TestViewModel by viewModels(ownerProducer = { rootFragment }) } fun Fragment.findRootFragment(predicate: (Fragment) -> Boolean): Fragment { retu.. [Android] Hilt: Dependency Injection Made Easy 주의! 본 포스팅은 ChatGPT를 통해 만들었습니다 Caution! This Posting was made with ChatGPT Using Hilt for Dependency Injection in Android As your Android app grows in complexity, you may find yourself dealing with dependencies between classes that are hard to manage and test. One solution to this problem is to use a dependency injection (DI) framework to help manage your dependencies and make your code more modu.. [Android] Dagger & Hilt ViewModelFactory Dagger를 통해 DI 구현 중 ViewModelProvider.Factory를 의존성 주입하여 구현하는 방법을 정리해본다. 코드들은 Kotlin으로 작성했다. import androidx.lifecycle.ViewModel import dagger.MapKey import java.lang.annotation.* import java.lang.annotation.Retention import java.lang.annotation.Target import kotlin.reflect.KClass @Documented @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @MapKey annotation class ViewModelKey(val .. 이전 1 다음