Forum Xamarin.Android

How to get context of ActivityCompat.RequestPermissions

dai15002dai15002 Member ✭✭✭

In my Xamarin.Form I call a function inside MainActivity to grant permission for WRITE_CALENDAR. However I need to get the right context to be passed in ActivityCompat.RequestPermissions funtion. My solution:

 public bool GrantPermission()

        if (hasPermission)
            // We have permission
            return true;
            int RC_REQUEST_PERMISSION = 1000;
            string[] REQUIRED_PERMISSIONS = { Android.Manifest.Permission.WriteCalendar };

            // USING this WILL CRASH
            ActivityCompat.RequestPermissions(this, REQUIRED_PERMISSIONS, RC_REQUEST_PERMISSION);


        return false;


How can I get an working activity context to send inside ActivityCompat.RequestPermissions?


  • dai15002dai15002 Member ✭✭✭

    fixed by changing this to CrossCurrentActivity.Current.Activity

  • Imran_619Imran_619 Member ✭✭✭

    The documentation on requestPermissions says that the activity parameter is the target activity where you want to show the pop up if you haven't included the permission in your manifest and for this purpose that method requires you to pass an activity and not the context, because upon finish the request permissions task it will then return a result to the calling activity(that is the activity passed as the parameter to the method). If you are so adamant about implementing this through your singleton I suggest you create a function that accepts the activity in the parameter and the callbacks too as you WILL need to handle the callbacks if the permissions were given or not

    For more information on mobile app development you can visit ArStudioz

Sign In or Register to comment.