Base Data Binding Dialog Fragment
A base DialogFragment class that uses DataBinding and provides common functionality for data-bound dialogs.
Extends ParentBindingViewDialogFragment to inherit binding lifecycle and ViewModel event collection.
DataBinding을 사용하고 데이터 바인딩 다이얼로그에 대한 공통 기능을 제공하는 기본 DialogFragment 클래스입니다.
ParentBindingViewDialogFragment를 확장하여 바인딩 생명주기와 ViewModel 이벤트 수집을 상속받습니다.
Why this class exists / 이 클래스가 필요한 이유:
Android's DataBinding requires manual DataBindingUtil.inflate() calls for each DialogFragment.
DataBinding enables automatic UI updates when LiveData values change, reducing boilerplate update code (StateFlow requires manual collection).
This class automatically sets lifecycleOwner to enable LiveData observation, which developers often forget.
Provides a centralized place for DataBinding-specific cleanup (unbind, null lifecycleOwner).
Android의 DataBinding은 각 DialogFragment마다 수동으로 DataBindingUtil.inflate() 호출이 필요합니다.
DataBinding은 LiveData 값이 변경될 때 자동 UI 업데이트를 가능하게 하여 보일러플레이트 업데이트 코드를 줄입니다 (StateFlow는 수동 수집 필요).
이 클래스는 개발자가 자주 잊는 LiveData 관찰을 활성화하기 위해 자동으로 lifecycleOwner를 설정합니다.
DataBinding 전용 정리(unbind, null lifecycleOwner)를 위한 중앙화된 장소를 제공합니다.
Design decisions / 설계 결정 이유:
Uses constructor parameter for layout resource ID to match Android's standard DataBinding pattern.
Automatically sets binding.lifecycleOwner in onViewCreated() to viewLifecycleOwner for proper view lifecycle binding.
Implements final createBinding() to prevent subclasses from breaking the DataBinding initialization contract.
Calls unbind() and nulls lifecycleOwner in onDestroyView() to prevent memory leaks from LiveData observers.
Android의 표준 DataBinding 패턴과 일치하도록 생성자 파라미터로 레이아웃 리소스 ID를 사용합니다.
적절한 뷰 생명주기 바인딩을 위해 onViewCreated()에서 자동으로 binding.lifecycleOwner를 viewLifecycleOwner로 설정합니다.
final createBinding()을 구현하여 하위 클래스가 DataBinding 초기화 계약을 깨는 것을 방지합니다.
LiveData 옵저버로 인한 메모리 누수를 방지하기 위해 onDestroyView()에서 unbind()를 호출하고 lifecycleOwner를 null로 설정합니다.
Important notes / 주의사항:
⚠️ CRITICAL: lifecycleOwner is automatically set to viewLifecycleOwner in onViewCreated(). Unlike Activity's BaseDataBindingActivity, you don't need to set it manually.
DataBinding automatically updates UI when LiveData values change - no need for manual observe() calls in XML-bound properties (StateFlow requires manual collection).
Always use repeatOnLifecycle(Lifecycle.State.STARTED) inside onEventVmCollect(binding:BINDING) to properly handle configuration changes.
Access the binding object via getBinding() method after super.onViewCreated() completes.
⚠️ 중요: lifecycleOwner는 onViewCreated()에서 viewLifecycleOwner로 자동 설정됩니다. Activity의 BaseDataBindingActivity와 달리 수동으로 설정할 필요가 없습니다.
DataBinding은 LiveData 값이 변경될 때 자동으로 UI를 업데이트합니다 - XML에 바인딩된 프로퍼티에 대해 수동 observe() 호출이 필요하지 않습니다 (StateFlow는 수동 수집 필요).
구성 변경을 올바르게 처리하려면 onEventVmCollect(binding:BINDING) 내부에서 항상 repeatOnLifecycle(Lifecycle.State.STARTED)를 사용하세요.
super.onViewCreated() 완료 후 getBinding() 메서드를 통해 바인딩 객체에 접근하세요.
Usage / 사용법:
Extend this class with your DialogFragment and pass the layout resource ID.
Access views through getBinding() in onViewCreated() or override onViewCreated(binding, savedInstanceState) for initialization.
Override onEventVmCollect(binding:BINDING) to collect ViewModel events with repeatOnLifecycle.
LiveData properties bound in XML will automatically update - no manual observation needed.
DialogFragment에서 이 클래스를 상속받고 레이아웃 리소스 ID를 전달하세요.
onViewCreated()에서 getBinding()을 통해 뷰에 접근하거나 초기화를 위해 onViewCreated(binding, savedInstanceState)를 오버라이드하세요.
repeatOnLifecycle과 함께 ViewModel 이벤트를 수집하려면 onEventVmCollect(binding:BINDING)를 오버라이드하세요.
XML에 바인딩된 LiveData 프로퍼티는 자동으로 업데이트됩니다 - 수동 관찰이 필요하지 않습니다.
Usage example:
class ConfirmDialog : BaseDataBindingDialogFragment<DialogConfirmBinding>(R.layout.dialog_confirm) {
private val viewModel: ConfirmViewModel by lazy { getViewModel() }
override fun onViewCreated(binding: DialogConfirmBinding, savedInstanceState: Bundle?) {
binding.viewModel = viewModel
// lifecycleOwner is already set automatically - no need to set it manually
}
override fun onEventVmCollect(binding:BINDING) {
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.dismissEvent.collect { safeDismiss() }
}
}
}
}Parameters
The type of the DataBinding class.
DataBinding 클래스의 타입.
The layout resource ID for the dialog.
다이얼로그의 레이아웃 리소스 ID.
Whether to attach the inflated view to the parent container.
인플레이션된 뷰를 부모 컨테이너에 첨부할지 여부.
DialogFragment에서는 특별한 경우가 아니라면 기본값 false 사용을 권장합니다.
See also
For the abstract parent class of all binding-enabled dialog fragments.
모든 바인딩 지원 DialogFragment의 추상 부모 클래스는 ParentBindingViewDialogFragment를 참조하세요.
For base class with dialog and permission features.
다이얼로그 및 권한 기능이 있는 기본 클래스는 RootDialogFragment를 참조하세요.
For simple layout-based DialogFragment without DataBinding.
DataBinding 없이 간단한 레이아웃 기반 DialogFragment는 BaseDialogFragment를 참조하세요.
For ViewBinding-enabled DialogFragment.
ViewBinding을 사용하는 DialogFragment는 BaseViewBindingDialogFragment를 참조하세요.
Properties
Functions
Called immediately after the binding object is created, inside onCreateView().
Use only for binding variable assignment (e.g. binding.vm = vm).
Do NOT access viewLifecycleOwner, start collectors, or call lifecycle-aware APIs here — viewLifecycleOwner is not yet available at this point.
onCreateView() 내부에서 바인딩 객체가 생성된 직후 호출됩니다.
바인딩 변수 할당(예: binding.vm = vm)에만 사용하세요.
viewLifecycleOwner 접근, collector 시작, lifecycle-aware API 호출은 금지입니다 — 이 시점에서는 viewLifecycleOwner를 아직 사용할 수 없습니다.
Called when the view previously created by onCreateView has been detached from the fragment.
Cleans up the binding reference to prevent memory leaks by setting lifecycleOwner to null and calling unbind().
onCreateView에서 생성된 뷰가 Fragment에서 분리될 때 호출됩니다.
lifecycleOwner를 null로 설정하고 unbind()를 호출하여 메모리 누수를 방지하기 위해 바인딩 참조를 정리합니다.
Called to start collecting ViewModel events.
Typically invoked once per lifecycle (e.g., onCreate or onViewCreated).
ViewModel 이벤트 수집을 시작할 때 호출됩니다.
보통 생명주기 당 1회(onCreate 또는 onViewCreated) 호출됩니다.
Called when the binding is initialized in onViewCreated().
Implement setup logic that requires binding here.
onViewCreated에서 바인딩이 초기화된 후 호출됩니다.
바인딩이 필요한 초기화 로직을 여기서 수행하세요.
Called immediately after onCreateView() has returned.
Sets the binding's lifecycleOwner to viewLifecycleOwner for proper LiveData observation tied to the view lifecycle.
onCreateView()가 반환된 직후 호출됩니다.
뷰 생명주기에 연결된 적절한 LiveData 관찰을 위해 바인딩의 lifecycleOwner를 viewLifecycleOwner로 설정합니다.
Requests permissions using the delegate.
Call only after the Fragment is attached (isAdded == true).
델리게이트를 사용하여 권한을 요청합니다.
Fragment가 attach된 이후(isAdded == true)에만 호출하세요.
Requests multiple permissions and returns denied results via callback.
여러 권한을 요청하고 거부 결과를 콜백으로 반환합니다.
Safely dismisses the dialog with exception handling.
Catches any exceptions that may occur during dismissal.
예외 처리와 함께 다이얼로그를 안전하게 닫습니다.
닫는 동안 발생할 수 있는 모든 예외를 잡습니다.
Safely shows the dialog with exception handling.
Catches any exceptions that may occur during showing.
예외 처리와 함께 다이얼로그를 안전하게 표시합니다.
표시하는 동안 발생할 수 있는 모든 예외를 잡습니다.
Sets the custom animation style for dialog appearance/disappearance.
Applied immediately if dialog is already showing.
다이얼로그 나타남/사라짐에 대한 커스텀 애니메이션 스타일을 설정합니다.
다이얼로그가 이미 표시 중인 경우 즉시 적용됩니다.
Sets whether the dialog can be canceled by pressing back button or touching outside.
Applied immediately if dialog is already showing.
뒤로 가기 버튼을 누르거나 외부를 터치하여 다이얼로그를 취소할 수 있는지 설정합니다.
다이얼로그가 이미 표시 중인 경우 즉시 적용됩니다.
Sets the position of the dialog on screen.
Applied immediately if dialog is already showing.
화면에서 다이얼로그의 위치를 설정합니다.
다이얼로그가 이미 표시 중인 경우 즉시 적용됩니다.
Creates and shows an indefinite duration Snackbar from a Fragment.
Logs an error if the Fragment's view is null.
Fragment에서 무제한 시간 Snackbar를 생성하고 표시합니다.
Fragment의 view가 null이면 에러를 로깅합니다.
Creates and shows a long duration Snackbar from a Fragment.
Logs an error if the Fragment's view is null.
Fragment에서 긴 시간 Snackbar를 생성하고 표시합니다.
Fragment의 view가 null이면 에러를 로깅합니다.
Creates and shows a short duration Snackbar from a Fragment.
Logs an error if the Fragment's view is null.
Fragment에서 짧은 시간 Snackbar를 생성하고 표시합니다.
Fragment의 view가 null이면 에러를 로깅합니다.
Creates and shows a long duration Toast message from a Fragment.
Logs an error if the Fragment's context is null.
Fragment에서 긴 시간 Toast 메시지를 생성하고 표시합니다.
Fragment의 context가 null이면 에러를 로깅합니다.
Creates and shows a short duration Toast message from a Fragment.
Logs an error if the Fragment's context is null.
Fragment에서 짧은 시간 Toast 메시지를 생성하고 표시합니다.
Fragment의 context가 null이면 에러를 로깅합니다.
Executes the given block and returns a result if the Fragment's context is not null.
Returns null and logs an error if the context is null.
Fragment의 context가 null이 아닌 경우 주어진 블록을 실행하고 결과를 반환합니다.
context가 null이면 null을 반환하고 에러를 로깅합니다.
Executes the given block and returns a result if the Fragment's view is not null.
Returns null and logs an error if the view is null.
Fragment의 view가 null이 아닌 경우 주어진 블록을 실행하고 결과를 반환합니다.
view가 null이면 null을 반환하고 에러를 로깅합니다.