requireActivity().onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
Log.d(TAG, "Fragment back pressed invoked")
if(viewModel.doCustomAction){
doCustomAction()
}
else{
isEnabled = false
requireActivity().onBackPressed()
}
}
}
)
这里,viewModel实例不是该片段使用的实例,因为它是一个回调。如何访问片段的同一viewModel实例?