Search code examples
androidkotlinandroid-fragmentsandroid-permissionsandroid-fragmentactivity

How to get permission result from activity in fragment?


I have MapFragment and I check for permission at onViewCreated:

  if (uiCommunicationListener.isLocationPermissionGranted()) {
        permission_button.visibility = View.GONE
        val mapFragment =
            childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
        mapFragment?.getMapAsync(callback)
    } else {
        //Hide Map
    }

uiCommunicationListener is an interface that responsible for fragments-activity communication.

isLocationPermissionGranted located in MainActivity and looks like this:

override fun isLocationPermissionGranted(): Boolean {
    if (ContextCompat.checkSelfPermission(
            this,
            android.Manifest.permission.ACCESS_FINE_LOCATION
        ) != PackageManager.PERMISSION_GRANTED) {
        //  Ask for permission
        ActivityCompat.requestPermissions(
            this,
            arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
            Constants.LOCATION_PREMMISION_CODE
        )
        return false
    } else {
        // Permission has already been granted
        return true
    }

Basically it is checking if permission is granted, if it is return true, if it's not ask for permission and return false.

The problem is that I can't do operation based on user action after we ask him for permissions.

In my case I'm checking if permission has been granted, if it is show the map if it's not hide the map.

But if it return false, and after the request permission dialog pops up and the user grant us permission, nothing happen because we already "Passed" the if check

Now Usually in onRequestPermissionsResult method I can make operation based on user actions(Grant us permssion/Deny the request) but here this method lives inside MainActivity and I can't get access to the request code, grant result etc.

How can I make operations in the fragment based on the result of request permission dialog?


Solution

    1. You have onRequestPermissionsResult in Fragment as well.

    2. If you still want to have onRequestPermissionsResult centrally in your Activity, you need to call the method of Fragment onRequestPermissionsResult of your Activity

      @Override
       public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
           switch (requestCode) {
               case ACCESS_FINE_LOCATION_PERM: {
                   // If request is cancelled, the result arrays are empty.
                   if (grantResults.length > 0
                       && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                       // permission was granted, do your work....
      
                       YourFragmentClass fragment = 
                       (YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
                       fragment.yourPublicMethod();
      
                   } else {
                       // permission denied
                       // Disable the functionality that depends on this permission.
                   }
                   return;
               }
      
               // other 'case' statements for other permssions
           }
       }
      

    Note : There are other ways as well to interact with Fragment from Activity.