2

I'm using google maps so I need to ask some permissions.
I'm requesting the user during the onStart() of the Fragment
I'm calling requestPermissions because I'm in a fragment and I also have called super.onRequestPermissionsResult as recommended in this answer and as you can see in the code below.
I have put some breakpoint and Log to be 100% sure that onRequestPermissionsResult is not called.

class FindLessonFragment : BaseFragment(), OnMapReadyCallback, OnClusterItemClickListener<ClusterLesson>, OnClusterClickListener<ClusterLesson> {

    override fun onStart() {
        super.onStart()
    checkPermissions()
    }

    private fun checkPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (activity!!.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    || activity!!.checkSelfPermission(permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    || activity!!.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                val builder = AlertDialog.Builder(activity!!)
                builder.setTitle("This app needs location access")
                builder.setMessage("Please grant location access so this app can work normally.")
                builder.setPositiveButton(android.R.string.ok, null)
                builder.setOnDismissListener {
                    requestPermissions(
                            arrayOf(permission.ACCESS_COARSE_LOCATION, permission.READ_EXTERNAL_STORAGE, permission.ACCESS_FINE_LOCATION),
                            PERMISSION_REQUEST_LOCATION)
                }
                builder.show()
            }
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, @NonNull permissions: Array<String>, @NonNull grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        Log.e("PERMISSIONRESULT", "PASS PERMISSION RESULT")
        when (requestCode) {
            BaseActivity.PERMISSION_REQUEST_LOCATION -> {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d("BaseActivity", "coarse location permission granted")
                    onMapReady(mMap)
                } else {
                    val builder = AlertDialog.Builder(context!!)
                    builder.setTitle("Functionality limited")
                    builder.setMessage(
                            "Since location access has not been granted, this app will not be able to work correctly.")
                    builder.setPositiveButton(android.R.string.ok, null)
                    builder.setOnDismissListener { dialog -> dialog.dismiss() }
                    builder.show()
                }
            }
        }
    }

    companion object {

        fun create() = FindLessonFragment()

        internal const val PERMISSION_REQUEST_LOCATION = 1
    }

}
minSdkVersion 23

Any kind of help is welcome

Itoun
  • 3,713
  • 5
  • 27
  • 47
  • Are you sure that you are making your `requestPermissions()` call? Also, are you sure that you have all three of those permissions in the manifest? Note that AFAIK you don't need both `ACCESS_FINE_LOCATION` and `ACCESS_COARSE_LOCATION` -- `ACCESS_FINE_LOCATION` alone would suffice. – CommonsWare Jun 18 '19 at 21:30
  • yes of course because I have the request permissions displaying on the app, I even set a breakpoint on it. @CommonsWare – Itoun Jun 18 '19 at 21:50
  • 3
    Have you overriden `onRequestPermissionsResult` in your Activity? Can you include that code if so? – ianhanniballake Jun 19 '19 at 04:44
  • I think that @ianhanniballake has got a valid point. According to answer linked by you, and also to https://stackoverflow.com/a/33080682/1621904, make sure to catch result in activity. – koto Jun 19 '19 at 08:28
  • I didn't get this part actually ! I have put `onRequestPermissionsResult` and now it's working like a charm ! Please put your comment as an answer @ianhanniballake Thank you also @koto – Itoun Jun 19 '19 at 11:14

1 Answers1

0

The onRequestPermissionsResult has to be catch in the activty.

Itoun
  • 3,713
  • 5
  • 27
  • 47