0

I am trying to make a phone call from the dialog fragment which is inside another fragment. However, my onRequestPermissionsResult() is not being called, whenever I choose allow or deny, it doesn't react. Here is my code:

private val PHONE_REQUEST_CODE = 100;
private lateinit var phonePermission: Array<String>

     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
             btnOk.setOnClickListener(this)
             btnCancel.setOnClickListener(this)
             dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    phonePermission = arrayOf(android.Manifest.permission.CALL_PHONE)


}

    override fun onClick(v: View?) {
    when (v?.id) {
        R.id.btnOk -> {
            dismiss()
            makePhoneCall()
        }

        R.id.btnCancel -> {
            dismiss()
        }
    

    }
}


override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
    if (requestCode == PHONE_REQUEST_CODE) {
      
          if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            val intent = Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "111 111 111"))
            startActivity(intent)
        
        }
    }

}


private fun makePhoneCall(){
    if (ContextCompat.checkSelfPermission(
            requireContext(),
            android.Manifest.permission.CALL_PHONE
        ) != PackageManager.PERMISSION_GRANTED
    ) {
        requestPermissions(
            phonePermission,
            PHONE_REQUEST_CODE
        )

    } else {
        val intent = Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "111 111 111"))
        startActivity(intent)
    }

}

}

I have tried several solutions offered in stackoverflow for similar problems, such as replacing ActivityCompat.requestPermissions() with just requestPermissions(). But still it didn't work. Thanks in advance

Benazir Sh
  • 81
  • 10

2 Answers2

0

Fragment's "onRequestPermissionsResult" will be called if your hosting activity passed the onRequestPermissionsResult call to base activity.

  • Thanks for trying to help me. This dialog fragment is being hosted by another fragment, not activity – Benazir Sh Nov 30 '20 at 13:55
  • Basically, I have a main fragment with icon. When the icon is clicked, I am calling dialog fragment with ok and cancel buttons. When ok is clicked, the permission is being asked, and when allow is clicked, it doesn't affect the dialog, it doesn't make a phone call. I have to click the dialog fragment again and call ok one more time to make a phone call. So, onRequestPermissionsResult is not being called at all, I checked it via Logs – Benazir Sh Nov 30 '20 at 13:59
  • whos requestPermissions method are you calling? Activity's or fragments? – Олег Казьмин Nov 30 '20 at 16:39
  • calling fragment's – Benazir Sh Nov 30 '20 at 17:43
  • If you really need permission callback you can call 'requestPermission' activity's method or hosting fragment's one. – Олег Казьмин Nov 30 '20 at 17:57
  • As Nadim Ansari mentioned below this seems to be a bug or something. If you really need a permission callback do a permission reqest via activity's or hosting fragment's 'requestPermission' method. Good luck! – Олег Казьмин Nov 30 '20 at 18:00
0

On fragment parent activity's onRequestPermissionsResult make sure to call super.onRequestPermissionsResult

Fazal Jarral
  • 160
  • 1
  • 14