【发布时间】:2021-04-09 09:47:26
【问题描述】:
使用新的 Fragment Result API 从其他 2 个 Fragment 获取结果的 Fragment F 怎么可能:A,B 从 A 但不是从 B 获取结果,因为 B 有不同的父 FragmentManager(我没有知道为什么) ?怎么可能是那样的? 2 个片段以相同的方式调用,但它们最终具有相同的 Activity 但不同的 FragmentManager ?函数调用如下:
//这不起作用。设置结果后不调用监听器
private fun navigateToItemLocation() {
setFragmentResultListener(REQUEST_LOCATION_KEY) { s: String, bundle: Bundle ->
val locationId = bundle.getParcelable<ParcelUuid>(LOCATION_ID)!!.uuid
viewModel.viewModelScope.launch(Dispatchers.IO) {
val location = LocationRepository().get(locationId)!!
changeItemLocation(location)
}
}
val action = ItemRegistrationPagerHolderDirections.actionNavItemRegistrationPagerToNavStockLocationSelection()
findNavController().navigate(action)
}
//这工作正常:
private fun navigateToItemDetails(item: Item2) {
setFragmentResultListener(SELECTED_ITEM_KEY) { s: String, bundle: Bundle ->
val propertySetId = bundle.getParcelable<ParcelUuid>(SELECTED_ITEM_SET_ID)!!.uuid
clearFragmentResultListener(SELECTED_ITEM_KEY)
viewModel.viewModelScope.launch(Dispatchers.IO) {
val repository = PropertySetRepository()
val propertySet = repository.get(propertySetId)!!
val propertySetInfo = ItemFactory.loadPropertySetInfo(propertySet)
withContext(Dispatchers.Main) { setPackageCode(null) }
selectItem(item.item, propertySetInfo, item.description, null)
}
}
val action = ItemRegistrationPagerHolderDirections.actionNavItemRegistrationToNavStockItemDetails(ParcelUuid(item.item.id), true)
findNavController().navigate(action)
}
片段 A 和 B 都在单独的动态特征中。我唯一的问题是当调用以下函数时:
fun onSelect() {
viewModel.pickedLocation.value = (viewModel.selectedLocation as? LocationExt2?)?.location
val result = bundleOf(Pair(LOCATION_ID, ParcelUuid(viewModel.pickedLocation.value!!.id)))
setFragmentResult(REQUEST_LOCATION_KEY, result)
findNavController().popBackStack()
}
setFragmentResult(REQUEST_LOCATION_KEY, 结果)
不产生任何结果,因为 FragmentManager 与调用 Fragment 不同。片段A中的相同方法是:
private fun onSetSelected(id: UUID) {
propertySets.removeObservers(viewLifecycleOwner)
adapter.tracker = null
setFragmentResult(SELECTED_ITEM_KEY, bundleOf(Pair(SELECTED_ITEM_SET_ID, ParcelUuid(id))))
findNavController().popBackStack()
}
作为临时解决方法,我用 Activity.supportFragmentManager.setFragmentResultListener 替换了对 Fragment 的 FragmentManager 的调用。它有效,但我仍然不明白为什么片段 A 和 B 的行为不同......
【问题讨论】:
标签: android android-fragments navigation fragment