question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New ActivityResultContracts.RequestPermission() sample

See original GitHub issue

Issue

Android 11

With the new Android 11, we have a change on permissions behaviour, adding:

Luckily, Android already give a method(RequestPermission) that deal with all this cases.

Developer Android Permission Guide

On the permission guide Request App Permission where this project is linked (end of the page) we can see the usage of ActivityResultContracts.RequestPermission() and ActivityResultCaller.registerForActivityResult.

What can make the developer confused because registerForActivityResult still on alpha for appCompat library.

Solution

Based on this, would be beneficial for the community to have a sample code for it. But since the library still not release this should be another sub-project until the appCompat release 1.3 and this code became the default one.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kovrizhincommented, Feb 9, 2021

@Canato Yeah, thanks a lot for. Yep, I do the first approach right now and trying to find good solution, how to correct handle it. The second of course is better, but it is required a lot of staff just for small things, and if you have quite simple application it is a big pain 😃

But thanks a lot. 😃

0reactions
ImMuhammadHasibcommented, Dec 23, 2021

@SuppressLint(“MissingSuperCall”) override fun onRequestPermissionsResult( requestCode: Int, permissions: Array<out String>, grantResults: IntArray ) { if (requestCode == myResquestCode) { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { getLastLocation() } else { requestPermission() } } }

Read more comments on GitHub >

github_iconTop Results From Across the Web

Requesting runtime permissions using new ActivityResult API
ActivityResultCallback will return map with permission as key and its grant status as value e.g. below example prints android.permission.
Read more >
How to get a permission request in new ActivityResult API (1.3 ...
I´m always getting false in registerForActivityResult(). // Permission to get photo from gallery, gets permission and produce boolean private ...
Read more >
ActivityResultContracts.RequestPermission
ActivityResultContract.SynchronousResult<@NonNull Boolean>. the result wrapped in a SynchronousResult or null if the call should proceed to start an activity.
Read more >
Activity Result Contract – The Basics - Styling Android
We can use this new pattern wherever we previously used startActivityForResult() – it is not restricted to runtime permissions. For example ...
Read more >
Request Permission with new API - Technical difficulties
ACCESS_FINE_LOCATION));. Job done. If you have to ask multiple permission, don't call ActivityResultContracts.RequestPermission() but call ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found