How do I accrss a fragment's button click inside my activity?

I'm a new Xamarin developer, using Visual Studio 2015, with the DesignCompat Library
I have a MainActivity that replaces fragments.

One of my fragments contains a list of buttons. These buttons need to load other fragments.
So I thought I would display the buttons, then handle the button's click event inside the MainActivity. That sounded right to be because the MainActivity could load the fragments and be the traffic cop.
I could not find a way to access the MainActivity from the Fragment, ir the Fragment's buttons from the MainActivity.
I could convert this all to separate activity's but I thought that was wrong.

But I can't seem to get a handle to the button object. It tells me object is null.

Best Answer

Yes I was able to figure it out but in the C# way. I created an event handler and raised an event which I handled in the main activity.

I have all my regular buttons just like as if I was handling them in the fragment, but I pointed all of the click events to one function:

In my Fragment (the mFragMyActivities class):

public class FragMyActivities : Android.Support.V4.App.Fragment
{
//....
// Set up the event handler at the top of the class
public event EventHandler OptionButtonWasClicked;
// Get all of the buttons
btnOptionOne = view.FindViewById(Resource.Id.btnOptionOne );
btnOptionTwo = view.FindViewById(Resource.Id.btnOptionTwo );
btnOptionThree = view.FindViewById(Resource.Id.btnOptionThree );
// ...
public class FragMyActivities : Android.Support.V4.App.Fragment
{
//....
// Set up the event handler at the top of the class that calls the one in MainActivity
public event EventHandler OptionButtonWasClicked;
// Get all of the buttons
btnOptionOne = view.FindViewById(Resource.Id.btnOptionOne );
btnOptionTwo = view.FindViewById(Resource.Id.btnOptionTwo );
btnOptionThree = view.FindViewById(Resource.Id.btnOptionThree );
// ...
// Set up the click events all pointing to one method
btnOptionOne.Click += BtnOption_Click;
btnOptionTwo.Click += BtnOption_Click;
btnOptionThree.Click += BtnOption_Click;
// The function that handles all the button clicks
private void BtnOption_Click(object sender, EventArgs e)
{
// Fire the event to the MainActivity
OptionButtonWasClicked(this, sender);
}

Yes I was able to figure it out but in the C# way. I created an event handler and raised an event which I handled in the main activity.

I have all my regular buttons just like as if I was handling them in the fragment, but I pointed all of the click events to one function:

In my Fragment (the mFragMyActivities class):

public class FragMyActivities : Android.Support.V4.App.Fragment
{
//....
// Set up the event handler at the top of the class
public event EventHandler OptionButtonWasClicked;
// Get all of the buttons
btnOptionOne = view.FindViewById(Resource.Id.btnOptionOne );
btnOptionTwo = view.FindViewById(Resource.Id.btnOptionTwo );
btnOptionThree = view.FindViewById(Resource.Id.btnOptionThree );
// ...
public class FragMyActivities : Android.Support.V4.App.Fragment
{
//....
// Set up the event handler at the top of the class that calls the one in MainActivity
public event EventHandler OptionButtonWasClicked;
// Get all of the buttons
btnOptionOne = view.FindViewById(Resource.Id.btnOptionOne );
btnOptionTwo = view.FindViewById(Resource.Id.btnOptionTwo );
btnOptionThree = view.FindViewById(Resource.Id.btnOptionThree );
// ...
// Set up the click events all pointing to one method
btnOptionOne.Click += BtnOption_Click;
btnOptionTwo.Click += BtnOption_Click;
btnOptionThree.Click += BtnOption_Click;
// The function that handles all the button clicks
private void BtnOption_Click(object sender, EventArgs e)
{
// Fire the event to the MainActivity
OptionButtonWasClicked(this, sender);
}

There's a different way. Get the MainActivity object and use it to call a method in the main activity that you can pass the info to, but I'm not sure its the correct way. The event seems cleaner and it stays disconnected.

Gary, I've been having the exact same problem all day. Not sure why this is so hard but I also cannot find a single line of code that works. I'm curious on your FragMyActivities class, it seems like you have some code duplicated. Could you clarify? I'm trying to see if I could use your solution but that part doesn't seem right.

Is possible to have a click event that takes you from a fragment to a new activity.
I spend hours on the same problem but finally found a simple solution to it. I quest this will help my collages developers who are also new to xamarin.

First, make sure you Get all of the buttons using:
Example: Button mButton = view.FindViewById(Resource.Id.button1);
Set up the click event:
Dont forget to return you view if not it will not work And you are good to go! Note! All these will be done in the OnCreateView and the "return view; " will be the last //thing in the OnCreateView. Contact me on: cdorkye@gmail.com if you dont understand this.