CoderJony

Open activity from fragment in Xamarin Android

You can use below code to open an activity from fragment in Xamarin Android.

Intent intent = new Intent(this.Activity, typeof(HomeDemo));
StartActivity(intent);

Here is the complete code:

public class HomeFragment : Fragment
{
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Use this to return your custom view for this Fragment
            View view = inflater.Inflate(Resource.Layout.HomeFragmentLayout, container, false);

            Button btnHome = view.FindViewById<Button>(Resource.Id.btnHome);
            btnHome.Click += BtnHome_Click;

            return view;
        }

        private void BtnHome_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this.Activity, typeof(HomeDemo));
            StartActivity(intent);
        }
}
Buy Me A Coffee