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.

I've been googling this for 2 hours.
Any help would be great.

Best Answer

Answers

  • rachelbiddlerachelbiddle GBMember

    Did you find any answers to this as I have same problem, everything I find is in Java and I'm coding in C#

  • GaryParkinGaryParkin USMember ✭✭

    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.

  • eddyizmeddyizm USMember ✭✭

    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.

  • TalkTrueTalkTrue GHMember

    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.

    Button mButton = view.FindViewById(Resource.Id.button1);

    SignupObtionA.Click += (object sender, EventArgs e) =>
    {
    var intent = new Intent(Activity, typeof(YourNewActivity));
    StartActivity(intent);
    };

    return view;

Sign In or Register to comment.