How to Manage the permission request code using Navigation Component?
I referred too many website & stack links, none of it worked, still showing deprecated Waring
Google Ref link: https://developer.android.com/training/permissions/requesting#manage-request-code-yourself
Code: :Using below code in fragment
private fun checkMultiplePermissions() {
// check permission first
if (ActivityCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// request the permission
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 100)
} else {
proceedAfterPermission() // has the permission.
}
}
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<String>, grantResults: IntArray) {
myLog("log", "location code : $requestCode")
when (requestCode) {
100 -> {
// If request is cancelled, the result arrays are empty.
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted.
proceedAfterPermission() // permission was granted.
myLog("log", "location granted")
} else {
// permission denied.
myLog("log", "location denied")
}
return
}
}
}
Fragment.kt : full code
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
class FragmentPermission : Fragment() {
private var binding: FragmentPermisionBinding ?= null
// Kotlin
//implementation "androidx.fragment:fragment-ktx:1.3.4"
//https://developer.android.com/training/permissions/requesting#manage-request-code-yourself
// <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
binding = FragmentPermisionBinding.inflate(inflater, container, false)
if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// request the permission
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 100)
} else {
//proceedAfterPermission() // has the permission.
}
return binding!!.root
}
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<String>, grantResults: IntArray) {
myLog("location3", "location code : $requestCode")
when (requestCode) {
100 -> {
// If request is cancelled, the result arrays are empty.
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted.
//proceedAfterPermission() // permission was granted.
myLog("location3", "location granted")
} else {
// permission denied.
myLog("location3", "location denied")
}
return
}
}
}
override fun onDestroyView() {
super.onDestroyView()
binding=null
}
}
requestPermissions()
API is deprecated, and replaced byregisterForActivityResult(ActivityResultContract, ActivityResultCallback)
passing in aActivityResultContracts.RequestMultiplePermissions
object for theActivityResultContract
and handling the result in the callback.First the activity-ktx
build.gradle
module level dependency is needed for the the new API:Then
ActivityResultCallback
callback need to be implemented and passed as a parameter of theregisterForActivityResult
. This interface has callbacks that handle the user response to the permission request dialog.To display the permissions dialog, call the
launch()
method on the instance ofActivityResultLauncher
that is returned by theregisterForActivityResult
function.So, create an instance of
ActivityResultLauncher
& implement the callback to handle the user response:Then request the permission by using the
launch
function:UPDATE
For requesting multiple of permissions at once: You need to pass array of permission to launch().
ActivityResultCallback
returns aMap<String, Boolean>
with permission as key and its grant status as value:Then use
ActivityResultContracts.RequestMultiplePermissions()
as follows:Applying that on the shared code: