user3535054
user3535054

Reputation: 77

Starting activity from Fragment doesn't work Xamarin

I'm trying to start an activity from fragment with Xamarin.Android. This is the fragment

public class ChoirFragment : TourFragment
{
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = inflater.Inflate(page, container, false);
        Button button = view.FindViewById<Button>(Resource.Id.Button);
        button.Click += delegate
        {
            var intent = new Intent(Activity, typeof(StartActivity));
            StartActivity(intent);
        };
        return base.OnCreateView(inflater, container, savedInstanceState);
    }
}

When I click on button, the click does nothing :(.

And I don't know why debugger doesn't stop at OnCreateView.

If you need more information do not hesitate to ask me. Thanks in advance!

Upvotes: 0

Views: 2109

Answers (2)

Try changing return base.OnCreateView(inflater, container, savedInstanceState); to return view

Upvotes: 0

Grace Feng
Grace Feng

Reputation: 16652

In your code return base.OnCreateView(inflater, container, savedInstanceState);, I don't know why this would work for you to load your fragment view, by my side I should return the view for layout.

Anyway, the following demo works for me:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    var view = inflater.Inflate(Resource.Layout.Choir_Fragment, container, false);
    Button button = view.FindViewById<Button>(Resource.Id.myButton);
    button.Click += delegate
    {
        //var intent = new Intent(this.Context, typeof(StartActivity));
        var intent = new Intent(Activity, typeof(StartActivity));
        StartActivity(intent);
    };
    return view;
}

Here is more info about my project: enter image description here

If this demo is needed, please leave a comment.

Upvotes: 1

Related Questions