如何解决片段未在后台堆栈中注册
// 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。我认为您应该将 String 值作为要使用的 backStack 的名称传递,而不是 null。
我不是 100% 确定这一点。您可以从此处阅读有关片段交易的更多信息:https://developer.android.com/guide/fragments/transactions
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。