I have DialogFragment in my project(MVVM, Jetpack navigation) that called from different places and represents signature canvas. Related part in navigation:
<dialog
android:id="@+id/signPadDialogFragment"
android:name="com.ui.signpad.SignPadDialogFragment"
android:label="SignPadDialogFragment" />
<fragment
android:id="@+id/loginFragment"
android:name="com.ui.login.LoginFragment"
android:label="@string/login_label"
tools:layout="@layout/login_fragment">
<action
android:id="@+id/action_loginFragment_to_currentJobsFragment"
app:destination="@id/currentJobsFragment" />
<action
android:id="@+id/action_loginFragment_to_signPadDialogFragment"
app:destination="@id/signPadDialogFragment" />
<fragment
android:id="@+id/jobDetailFragment"
android:name="com.ui.jobdetails.JobDetailFragment"
android:label="job_detail_fragment"
tools:layout="@layout/job_detail_fragment" >
<action
android:id="@+id/action_jobDetailFragment_to_signPadDialogFragment"
app:destination="@id/signPadDialogFragment" />
</fragment>
and navigate action:
mainActivityViewModel.repository.navigationCommands.observe(this, Observer { navEvent ->
navEvent.getContentIfNotHandled()?.let {
navController.navigate(it as NavDirections)
}
})
So, my question is: what is the right way to handle callbacks using Jetpack navigation and MVVM? I see two possible solution and related questions:
I can pass data to ViewModel -> Repository from dialog fragment( and in this case: how to differ action that started dialog inside dialog scope?)
Or get a callback in MainActivity(How?)
Thanks in advance