Hi, I want to request permissions for my app. Can ...
# compose
j
Hi, I want to request permissions for my app. Can I use
registerForActivityResult
function in Compose? In this section of the android documentation it says that you must always call
registerForActivtyResult
in the same order in your Fragment/Activity. Can this be achieved in Compose?
l
Short answer is yes, but dealing with it properly might be nontrivial. @Sean McQuillan [G] has been playing around with this recently and probably has some good direction to provide
a
yep, we designed the new androidx.activity to be able to work well with compose for this 🙂 Sean and I started playing with this from this sort of angle as a proof of concept: https://gist.github.com/adamp/be8cb9c26bb9d198873f9d04d45c9355
(there are probably a couple janky bugs in that gist)
s
Here's another implementation from brainstorming worth looking at https://gist.github.com/objcode/775fe45127fd40f17932f672ee203f72
👍 1
The big insight here is that you need to provide a unique key if you can't guarantee the calls are in the same order (which you can't in Compose).
☝️ 1
The default implementation just uses an incrementing atomic int to generate keys
a
Right. You can compute the key yourself, have Compose help you compute one, or some combination of the two.
j
Thanks for the quick answers! Very interesting examples
a
you happened to ask some questions about something we've been playing with in the past week or two anyway 😄
s
Adam where's the mystery
Coulda let everyone think we produced hundreds of lines of reviewed code in an hour 😄
a
"reviewed" 😅
s
😂
l
The default implementation just uses an incrementing atomic int to generate keys
@Sean McQuillan [G] That means the key will change if process is killed by the OS, preventing the result to return at the right place, right?
a
Yes, which is why Compose-based approaches shouldn't use that API entry point. Anything that isn't configuring itself unconditionally as part of initialization should generate a more stable key or require one to be supplied. (this is the one in particular to avoid in these cases: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:activity/activity/src/main/java/androidx/activity/result/ActivityResultCaller.java;l=39)
s
Correct, you can specify a unique key yourself if you use the register method like so, https://gist.github.com/objcode/775fe45127fd40f17932f672ee203f72#file-permissions-kt-L78
a
Is the above link still the best way to manage permissions in Compose ??? Publishing on the App Store and what to make sure doing the best way ... 🙏🏾
s
Yes and bump the libraries the gist is out of date a bit
🙏🏽 1
s
Man just to get a permission we have to do a lot of dancing . Things like requesting permission, choosing a file(supporting android 5+ ) are such common tasks but the amount of code we need to write is just Insane
l
@Samir Basnet I do a single (suspending) function call to request permissions blob wink
s
@louiscad yup we can always use extension/helper functions for reusing purpose but this should be provided by the platform api by in an elegant way. Beginners get lost with the api's provided by the current platform api
l
You mean AndroidX. No one would wait for minSdk whatever to get this simplified.
I agree that it can lose beginners and waste time…
👍 1