简体   繁体   中英

Android Data Binding. Button onClick not working

I am stuck here, help.

    @AndroidEntryPoint
    class ProfileFragment : Fragment() {
        private val profileViewModel: ProfileViewModel by viewModels()
    
        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            val binding = FragmentProfileBinding.inflate(inflater, container, false)
            binding.viewModel = profileViewModel
            binding.lifecycleOwner = viewLifecycleOwner
    
            return binding.root
        }
    }

I am not sure if this issue still exist or not but if you are using data binding and hilt for dependency injection please add below lines in your fragment onViewCreated

binding.lifecycleOwner = this
binding.viewModel = profileViewModel

What fixed this problem for me was adding the fragment reference to itself in onCreateView<\/em> :

binding.<fragment_name> = this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM