【发布时间】:2021-12-28 22:29:39
【问题描述】:
我有一个底部导航应用程序,我们需要能够从一个片段转到另一个片段(使用后退按钮返回第一个片段)。 我不知道要为容器视图 ID 传递什么以传递给替换调用。我尝试过的所有内容都显示未解决的参考。
class FoodLogFragment : Fragment() {
private var _binding: FragmentFoodlogBinding? = null
private lateinit var adapter: MealAdapter
var startDate: Date = Date()
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentFoodlogBinding.inflate(inflater, container, false)
val root: View = binding.root
return root
}
fun handleAddButton(mealType: MealType) {
val transaction = requireActivity().supportFragmentManager.beginTransaction()
//problem is here
//there are no layout ids after R.id, or R.layout
transaction.replace(R.id.fragment_foodcategory, FoodCategoryFragment())
transaction.disallowAddToBackStack()
transaction.commit()
}
【问题讨论】:
-
您使用的是
requireActivity().supportFragmentManager,因此 ID 必须是您活动布局中的容器。请包含您的活动的布局 XML。
标签: android kotlin android-fragments