New ActivityResultContracts.RequestPermission() sample
See original GitHub issueIssue
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:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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. 😃
@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() } } }