-
Notifications
You must be signed in to change notification settings - Fork 269
New ActivityResultContracts.RequestPermission() sample #7
Comments
Hello, also will be quite interesting to see, how to pass parameters from launch to callback. Because in early version it was easy to do with closures, but now it is not possible to do, because you have to register your callback when you create activity or fragments. Should we build Custom Contract, and where store values? Thanks. |
@kovrizhin could you take a look in the PR? Would this solve your question? If not, can you provide some code sample from the way you said in early version so I can help to find the latest way of doing it? |
@Canato Thanks a lot for quick response. I checked #8 and looks like I don't find answer for now. I understood a bit that it is wrong how I want to use and how I use it, this is why it was deprecated and not allowed, but it was quite useful. I will try to write code: Previously it was possible to write code like this, for example we have a RecycleView and in onBindViewHolder we create a closure, and provide item like this:
but now I need to check permission firstly and if I have it I can continue if not I need to ask it, when I request it loose context I don't have ability to continue action with parameters after user grant me permission, the user have click again action.
And I can't find how to do it in correct way, because creating custom contract will also not solve it, where is no place to store custom date inside to contract. Of course there are two possible hacks :) But I don't like it. You can store item into instance of your class or in the custom contract when do createIntent, but this I don't like.. |
I see your point. Indeed you could create a wrapper with custom contract, but for your case I agree is not the best solution, my personal suggestion (be clear I'm not part of the google team)
This second solution bring more work cause you would need to bring everything in another place following your architecture. But this will make your code safer and reliable on the long term. |
@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. :) |
Change-Id: I11cae937a03e05b85f43e0d78a20a50e0d255d21
@SuppressLint("MissingSuperCall") |
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()
andActivityResultCaller.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.The text was updated successfully, but these errors were encountered: