Skip to content Skip to sidebar Skip to footer

How To Get A Permission Request In New Activityresult Api (1.3.0-alpha05)?

so, I tried to get a permission with the new registerForActivityResult() method and ask for with button click with .launch() and it doesn´t seem to be opening any window to ask fo

Solution 1:

UPDATE This answer works, but I found a better solution for permission requests with no open holes here.


From docs:

In your Activity/Fragment, create this field:

// Register the permissions callback, which handles the user's response to the// system permissions dialog. Save the return value, an instance of// ActivityResultLauncher, as an instance variable.private ActivityResultLauncher<String> requestPermissionLauncher =
    registerForActivityResult(new RequestPermission(), isGranted -> {
        if (isGranted) {
            // Permission is granted. Continue the action or workflow in your// app.
        } else {
            // Explain to the user that the feature is unavailable because the// features requires a permission that the user has denied. At the// same time, respect the user's decision. Don't link to system// settings in an effort to convince the user to change their// decision.
        }
    });

Somewhere in the same Activity/Fragment:

if (ContextCompat.checkSelfPermission(
        context, Manifest.permission.ACCESS_BACKGROUND_LOCATION) ==
        PackageManager.PERMISSION_GRANTED) {
    performAction(...);
} elseif (shouldShowRequestPermissionRationale(...)) {
    // In an educational UI, explain to the user why your app requires this// permission for a specific feature to behave as expected. In this UI,// include a "cancel" or "no thanks" button that allows the user to// continue using your app without granting the permission.
    showInContextUI(...);
} else {
    // You can directly ask for the permission.// The registered ActivityResultCallback gets the result of this request.
    requestPermissionLauncher.launch(Manifest.permission.ACCESS_BACKGROUND_LOCATION);
}

If you are getting unreasonable "Permission denied" all the time, maybe you did not declare it in your manifest.xml?

Solution 2:

Looking at Update to androidx.fragment:fragment:1.3.0-alpha08: registerForActivityResult not allowed after onCreate anymore. How to use after onCreate?,

privatelateinitvar checkLocationPermission: ActivityResultLauncher<Array<String>>

// Initialize checkLocationPermission in onAttach or onCreate.overridefunonCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    checkLocationPermission = registerForActivityResult(
        ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
        if (permissions[Manifest.permission.ACCESS_FINE_LOCATION] == true ||
            permissions[Manifest.permission.ACCESS_COARSE_LOCATION] == true) {
            initUserLocation()
        } else {
            // Permission was denied. Display an error message.
        }
    }
}

funshowMap() {
    if (ActivityCompat.checkSelfPermission(requireContext(),
        Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
        ActivityCompat.checkSelfPermission(requireContext(),
            Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        initUserLocation()
    } else {
        checkLocationPermission.launch(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION))
    }
}

privatefuninitUserLocation() {
    googleMap?.isMyLocationEnabled = true
}

Post a Comment for "How To Get A Permission Request In New Activityresult Api (1.3.0-alpha05)?"