https://kotlinlang.org logo
#compose
Title
# compose
j

Julius Marozas

07/01/2020, 8:02 PM
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

Leland Richardson [G]

07/01/2020, 8:03 PM
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

Adam Powell

07/01/2020, 8:16 PM
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

Sean McQuillan [G]

07/01/2020, 8:27 PM
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

Adam Powell

07/01/2020, 8:39 PM
Right. You can compute the key yourself, have Compose help you compute one, or some combination of the two.
j

Julius Marozas

07/01/2020, 8:42 PM
Thanks for the quick answers! Very interesting examples
a

Adam Powell

07/01/2020, 8:43 PM
you happened to ask some questions about something we've been playing with in the past week or two anyway 😄
s

Sean McQuillan [G]

07/01/2020, 8:44 PM
Adam where's the mystery
Coulda let everyone think we produced hundreds of lines of reviewed code in an hour 😄
a

Adam Powell

07/01/2020, 8:45 PM
"reviewed" 😅
s

Sean McQuillan [G]

07/01/2020, 8:45 PM
😂
l

louiscad

07/02/2020, 8:26 AM
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

Adam Powell

07/02/2020, 3:05 PM
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

Sean McQuillan [G]

07/02/2020, 5:48 PM
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

Ash

03/08/2021, 6:25 PM
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

Sean McQuillan [G]

03/08/2021, 6:33 PM
Yes and bump the libraries the gist is out of date a bit
🙏🏽 1
s

Samir Basnet

03/29/2021, 12:41 PM
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

louiscad

03/29/2021, 12:42 PM
@Samir Basnet I do a single (suspending) function call to request permissions blob wink
s

Samir Basnet

03/29/2021, 12:49 PM
@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

louiscad

03/29/2021, 12:49 PM
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
2 Views