I am trying to create a registration page where there is return for a button that can be clicked to a login page and then I need to return a binding command for the registration form to the firebase but I cannot use two types of return because onCreateView only allows return to be implemented once. But the button's "return view" and the registration form's "return binding.root" are two different types of returns that cannot be combined.
How can I implement both of these returns at the same time?
Here is the code:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
val view: View = inflater!!.inflate(R.layout.fragment_register_email, container, false)
view.navigate_to_login_register_email.setOnClickListener { view ->
requireActivity().run {
startActivity(Intent(this, LogIn::class.java))
finish()
}
}
return view
binding = FragmentRegisterEmailBinding.inflate(layoutInflater, container, false)
binding.registerButtonEmail.setOnClickListener {
validateData()
}
return binding.root
CodePudding user response:
You're inflating the same layout twice, in two different ways. That's creating two copies, and you're setting one click listener in one layout, and another in the other layout. And your fragment can only use one of them for its view - whichever you pass back, it'll be missing a click listener.
If you're using view binding, and you're keeping a reference to the binding object (which you are, and that's how you'd normally do it) then just do this:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// generate your binding class by inflating the layout
binding = FragmentRegisterEmailBinding.inflate(layoutInflater, container, false)
// set your listeners on the views in the layout
binding.navigateToLoginRegisterEmail.setOnClickListener {
requireActivity().run {
startActivity(Intent(this, LogIn::class.java))
finish()
}
}
binding.registerButtonEmail.setOnClickListener {
validateData()
}
// return the root of the view hierarchy (i.e. the inflated layout)
return binding.root
}
