I'm moved from kotlin-extention
to binding usage as the extention
was deprecated so i'm rebuilding the whole app by using the bindings.
I have an issue with the accessing the BottomAppBar from fragment which is situated in my Activity.
Before bindings i was using the code like:
class CorpoFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(activity as LetturaActivity).bottomAppBar.navigationIcon = ContextCompat.getDrawable(
requireContext(),
R.drawable.ic_baseline_menu_24
)
}
}
But now i've set the binding in that fragment like:
class CorpoFragment : Fragment() {
private var fragmentCorpoBinding: FragmentCorpoBinding? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val binding = FragmentCorpoBinding.inflate(inflater, container, false)
fragmentCorpoBinding = binding
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// how can i access the bottomAppBar from the activity at this point?
}
After removing the kotlin-extention i get the Unresolved reference: bottomAppBar
on the first block of code..
The variables are accessible via the binding
class.
private lateinit var binding: FragmentCorpoBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentCorpoBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.bottomAppBar
}
I recommend you, not accessing the view elements from the Fragment to the Activity because every activity/fragment should have its self-independent view (separation of concern). If you really need it, just add something like this in your Activity:
class LetturaActivity {
fun updateBottomMenu() {
binding.bottomAppBar.navigationIcon = ContextCompat.getDrawable(
requireContext(),
R.drawable.ic_baseline_menu_24
)
}
}