简体   繁体   中英

Android onRequestPermissionsResult not being called in fragment

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

onRequestPermissionsResult必须抓在activty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM