0

I have been doing a tutorial that is a bit out of date and uses synthetics rather than bindings. I am trying to use bindins. I am trying to set up a listener in a fragment (AddEditFragment.kt). It's using a callback to MainActivity.onSaveClicked.

In AddEditFragment I use an import for the binding

import com.funkytwig.tasktimer.databinding.FragmentAddEditBinding

I have a lateinit on the first line of the class defenition

class AddEditFragment : Fragment() {
    private lateinit var binding: FragmentAddEditBinding

I am initializing the bunding in onActivityCreated and setting up the listner. I can use findViewById to get the ID

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    val addEditSave = view?.findViewById(R.id.addEditSave) as Button
    addEditSave.setOnClickListener { listener?.onSaveClicked() }
}

And this works fine but if I try to use the binding

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    binding.addEditSave.setOnClickListener { listener?.onSaveClicked() }
}

The code does not show any errrors but it does not seem to create the listner. I have a Log.d in the onSaveClicked callback function and when I use the first (findViewById) version of the function it works (it calles onSaveClicked) but with the second version (using bindings) onSaveClicked does not get called when I click the Button.

I Cant figre out why the second version does not work, I thought the two versions of onActivityCreated should do the same thing.

The interface in AddEditFragment.kt is

interface OnSaveClicked {
    fun onSaveClicked()
}
1
  • Are you also initializing your binding? Generally you do this in onViewCreated(:). It usually looks like binding = FragmentAddEditBinding.inflate(layoutInflater). After you did that it should work correctly. Commented Oct 10, 2022 at 14:03

2 Answers 2

4

In fragment you should add your view in onCreateView or in OnViewCreated not in onActivityCreated

Please refer link for more details.

private var _binding: FragmentAddEditBinding? = null
// 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 = FragmentAddEditBinding.inflate(inflater, container, false)
    val view = binding.root
 binding.addEditSave.setOnClickListener { listener?.onSaveClicked() }
    return view
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}
Sign up to request clarification or add additional context in comments.

Comments

0

OK, thanks for all the help. turned out I was doing the inflate wrong.

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.d(TAG, "onCreateView")
        binding = FragmentAddEditBinding.inflate(layoutInflater, container, false)
        return binding.root
}

I was doing

binding = FragmentAddEditBinding.inflate(layoutInflater)

I missed out on the last 2 args as I was taking the code from the inflate when I am in an Activity, not a Fragment. I think it is to do with the layout effecticly being in the parent.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.