[英]Pass data from Bottom Sheet Dialog Fragment to Fragment
I am using a BottomSheetDialogFragment class with Navigation Architecture component.我正在使用带有导航架构组件的 BottomSheetDialogFragment class。 I am following the Single activity pattern and therefore i have only one activity and several fragments.
我遵循单一活动模式,因此我只有一个活动和几个片段。 Below is my code.
下面是我的代码。
BottomSheetDialogFragment.kt 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
}
}
And this is how i open the dialog in my navigation.xml from my main fragment:这就是我在我的主要片段中打开我的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" />
Now i want to pass data FROM the bottom sheet to the main fragment.现在我想将底部工作表中的数据传递到主要片段。
Is there a proper way to do this?有没有合适的方法来做到这一点? Can someone please help me.
有人可以帮帮我吗。
Thank you.谢谢你。
As of Navigation 2.3.0-alpha02
, Navigation has built in support for Returning a result to a previous destination.从Navigation
2.3.0-alpha02
,Navigation 已内置支持将结果返回到先前的目的地。
This works in two parts, your first fragment (the one wanting to receive the result) would use navController.currentBackStackEntry?.savedStateHandle
to get a reference to the SavedStateHandle
associated with its NavBackStackEntry
in the NavController.这分为两部分,您的第一个片段(想要接收结果的片段)将使用
navController.currentBackStackEntry?.savedStateHandle
来获取对与其在NavBackStackEntry
中的SavedStateHandle
关联的 SavedStateHandle 的引用。 Then, it can observe
a particular key to get a callback whenever that key changes.然后,它可以
observe
特定键以在该键更改时获取回调。
The second fragment (the one delivering the result, ie, your LogoBottomSheetFragment
) would get a reference to that exact same SavedStateHandle
by using navController.previousBackStackEntry?.savedStateHandle
.第二个片段(传递结果的片段,即您的
LogoBottomSheetFragment
)将通过使用navController.previousBackStackEntry?.savedStateHandle
获得对完全相同的SavedStateHandle
的引用。 When that second fragment calls set
on the SavedStateHandle
, that result is then made available for the first fragment.当第二个片段调用
SavedStateHandle
上的set
时,该结果可用于第一个片段。
Note that there are some DialogFragment specific considerations to keep in mind - because the previous fragment is RESUMED
even when the BottomSheetFragment
is being shown, the result will instantly be sent to your first fragment.请注意,需要牢记一些DialogFragment 特定注意事项- 因为即使显示
RESUMED
,前一个片段也会被BottomSheetFragment
,结果将立即发送到您的第一个片段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.