Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 코틀린
- AppBarLayout
- Coroutine
- 알고리즘
- CustomView
- lifecycle
- room
- Algorithm
- hilt
- ViewModel
- CoordinatorLayout
- 알림
- Android
- Behavior
- notification
- 안드로이드
- BOJ
- CollapsingToolbarLayout
- onMeasure
- 백준
- kotlin
- DataBinding
- recyclerview
- View
- LiveData
- activity
- onLayout
- Navigation
- sqlite
- HTTP
Archives
- Today
- Total
개발일지
Android in A..Z - Hilt (Entry Point) 본문
Hilt Entry Point
Entry Point는 Hilt가 Inject하는 진입점이다. Activity, Fragment 같이 Android Framework에 의해 제공되는 Component들은 @AndroidEntryPoint를 통해 진입점을 지정할 수 있고, 일반 객체들도 @Inject constructor를 정의하여 진입점을 만들어 Inject할 수 있다.
하지만 Hilt가 지원하지 않는 Class에 Inject할 경우가 존재하고 이를 해결하기 위해 EntryPoint를 설정해야한다.
class DeleteToDoSnackbar(
private val view: View,
private val todo: ToDo
) {
private val entryPoint by lazy { EntryPoints.get(view.context.applicationContext, DeleteToDoSnackbarInterface::class.java) }
private val todoRepository by lazy { entryPoint.getToDoRepository() }
private val snackbar by lazy {
Snackbar.make(view, R.string.delete_success, Snackbar.LENGTH_LONG)
.setActionTextColor(view.context.resources.getColor(R.color.yellow_green, null))
.setAction(R.string.undo) {
todoRepository.insertToDo(todo)
}
}
fun show() {
snackbar.show()
}
@EntryPoint
@InstallIn(SingletonComponent::class)
interface DeleteToDoSnackbarInterface {
fun getToDoRepository(): ToDoRepository
}
}
- @EntryPoint과 @InstallIn로 EntryPoint를 생성한다. (InstallIn은 어떤 Scope에 해당하는지 정의)
- EntryPoint 접근 : EntryPoints.get()으로 EntryPoint에 접근한다. 첫번째 매개변수로 InstallIn으로 정의한 Scope를 넘겨준다.
- EntryPoint로 객체 접근
Git (예제소스)
github.com/KangTaeJong98/Example/tree/main/Android/Hilt
KangTaeJong98/Example
My Example Code. Contribute to KangTaeJong98/Example development by creating an account on GitHub.
github.com
'Android (안드로이드) > Hilt' 카테고리의 다른 글
Android in A..Z - Hilt (Component) (0) | 2021.03.18 |
---|---|
Android in A..Z - Hilt (Module) (0) | 2021.03.18 |
Android in A..Z - Hilt (개념) (0) | 2021.03.18 |
Comments