[英]How to pass data from adapter class to bottom sheet dialog fragment
[英]Pass data from Bottom Sheet Dialog Fragment to Fragment
我正在使用带有导航架构组件的 BottomSheetDialogFragment class。 我遵循单一活动模式,因此我只有一个活动和几个片段。 下面是我的代码。
BottomSheetDialogFragment.kt
class LogoBottomSheetFragment : BottomSheetDialogFragment() {
private var _binding: FragmentBottomSheetAccountLogoBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentBottomSheetAccountLogoBinding.inflate(inflater, container, false)
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
这就是我在我的主要片段中打开我的navigation.xml 对话框的方式:
<dialog
android:id="@+id/logoBottomSheetFragment"
android:name="com.th3pl4gu3.locky.ui.main.add.account.LogoBottomSheetFragment"
android:label="LogoBottomSheetFragment"
tools:layout="@layout/fragment_bottom_sheet_account_logo" />
现在我想将底部工作表中的数据传递到主要片段。
有没有合适的方法来做到这一点? 有人可以帮帮我吗。
谢谢你。
从Navigation 2.3.0-alpha02
,Navigation 已内置支持将结果返回到先前的目的地。
这分为两部分,您的第一个片段(想要接收结果的片段)将使用navController.currentBackStackEntry?.savedStateHandle
来获取对与其在NavBackStackEntry
中的SavedStateHandle
关联的 SavedStateHandle 的引用。 然后,它可以observe
特定键以在该键更改时获取回调。
第二个片段(传递结果的片段,即您的LogoBottomSheetFragment
)将通过使用navController.previousBackStackEntry?.savedStateHandle
获得对完全相同的SavedStateHandle
的引用。 当第二个片段调用SavedStateHandle
上的set
时,该结果可用于第一个片段。
请注意,需要牢记一些DialogFragment 特定注意事项- 因为即使显示RESUMED
,前一个片段也会被BottomSheetFragment
,结果将立即发送到您的第一个片段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.