How to set an OnClickListener in an activity to be received in a Fragment?

Viewed 29

I have a button in an activity (MainActivity) and I was wondering how to set an OnClick Listener for that in a fragment. I have tried interfaces and bundles but to no avail. Any help would be greatly appreciated!

1 Answers

You are trying to activity button click perform from a Fragment class

try this

((MainActivity) requireContext()).button.performClick();

MainActivity is your fragment's add activity and this button access modifier sets public to access in fragment

Related