片段未在后台注册
Fragment doesn't register in backstack
我有以下方法
// Change fragment extension function to handle navigation easily
fun changeFragment(fragmentManager: FragmentManager?, @IdRes containerId: Int, fragment: Fragment?, addToBackStack: Boolean = false) {
if (fragmentManager == null || fragment == null) return
val fragmentTransaction = fragmentManager.beginTransaction()
if (addToBackStack) fragmentTransaction.addToBackStack(null)
fragmentTransaction.replace(containerId, fragment, fragment::class.java.simpleName).commit()
}
我正在我的一个片段中使用它 -
class InspectionFragment : Fragment(R.layout.fragment_inspection) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
inspectionAdapter = InspectionAdapter { inspection ->
changeFragment(
parentFragmentManager, R.id.fragment_inspection_frame_layout,
InspectionDetailsFragment.newInstance(inspection), true
)
}
}
}
class InspectionDetailsFragment : Fragment() {
companion object {
fun newInstance(inspection: Inspection): InspectionDetailsFragment {
val inspectionDetailsFragment = InspectionDetailsFragment()
val bundle = Bundle()
bundle.putParcelable(GeneralConstants.INSPECTION, inspection)
inspectionDetailsFragment.arguments = bundle
return inspectionDetailsFragment
}
}
}
问题是,当我实例化 'InspectionDetailsFragment' 层的一个顶部时,另一个片段并按回,它直接返回到 'InspectionFragment' 父级。
我不明白为什么会这样。
有人有想法吗?
当您调用 changeFragment 函数时,您将 true 作为最后一个参数传递:
changeFragment(
parentFragmentManager, R.id.fragment_inspection_frame_layout,
InspectionDetailsFragment.newInstance(inspection), true
)
在changeFragment函数中:
if (addToBackStack) fragmentTransaction.addToBackStack(null)
它没有被添加到 backStack。而不是 null 我认为你应该传递字符串值作为你想要使用的 backStack 的名称。
我对此不是 100% 确定。您可以从此处阅读有关片段交易的更多信息:https://developer.android.com/guide/fragments/transactions
我有以下方法
// Change fragment extension function to handle navigation easily
fun changeFragment(fragmentManager: FragmentManager?, @IdRes containerId: Int, fragment: Fragment?, addToBackStack: Boolean = false) {
if (fragmentManager == null || fragment == null) return
val fragmentTransaction = fragmentManager.beginTransaction()
if (addToBackStack) fragmentTransaction.addToBackStack(null)
fragmentTransaction.replace(containerId, fragment, fragment::class.java.simpleName).commit()
}
我正在我的一个片段中使用它 -
class InspectionFragment : Fragment(R.layout.fragment_inspection) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
inspectionAdapter = InspectionAdapter { inspection ->
changeFragment(
parentFragmentManager, R.id.fragment_inspection_frame_layout,
InspectionDetailsFragment.newInstance(inspection), true
)
}
}
}
class InspectionDetailsFragment : Fragment() {
companion object {
fun newInstance(inspection: Inspection): InspectionDetailsFragment {
val inspectionDetailsFragment = InspectionDetailsFragment()
val bundle = Bundle()
bundle.putParcelable(GeneralConstants.INSPECTION, inspection)
inspectionDetailsFragment.arguments = bundle
return inspectionDetailsFragment
}
}
}
问题是,当我实例化 'InspectionDetailsFragment' 层的一个顶部时,另一个片段并按回,它直接返回到 'InspectionFragment' 父级。
我不明白为什么会这样。
有人有想法吗?
当您调用 changeFragment 函数时,您将 true 作为最后一个参数传递:
changeFragment(
parentFragmentManager, R.id.fragment_inspection_frame_layout,
InspectionDetailsFragment.newInstance(inspection), true
)
在changeFragment函数中:
if (addToBackStack) fragmentTransaction.addToBackStack(null)
它没有被添加到 backStack。而不是 null 我认为你应该传递字符串值作为你想要使用的 backStack 的名称。
我对此不是 100% 确定。您可以从此处阅读有关片段交易的更多信息:https://developer.android.com/guide/fragments/transactions