A Fragment represents a reusable portion of UI that lives inside an Activity (or another Fragment). Modern Android apps typically use a single Activity hosting multiple Fragments, rather than one Activity per screen.
class ProfileFragment : Fragment() {
private var _binding: FragmentProfileBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
_binding = FragmentProfileBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.profileNameText.text = "Ada Lovelace"
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null // avoid leaking the view past its lifecycle
}
}
The nullable _binding plus non-null binding getter is a standard pattern: a Fragment’s view can be destroyed and recreated independently of the Fragment object itself, so clearing the binding in onDestroyView prevents holding a reference to a destroyed view.
- Image
- SKU
- Rating
- Price
- Stock
- Availability
- Add to cart
- Description
- Content
- Weight
- Dimensions
- Additional information