Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can we override methods on the current activity? #66

Open
matthunz opened this issue Feb 17, 2023 · 1 comment
Open

How can we override methods on the current activity? #66

matthunz opened this issue Feb 17, 2023 · 1 comment

Comments

@matthunz
Copy link

Is it possible to override methods on the android activity?

For example I'm working on https://github.com/matthunz/permissions and I want to find a way to override onRequestPermissions for the activity. If this isn't possible yet I'm thinking a trait with these methods on it would be a cool idea

@rib
Copy link
Collaborator

rib commented Feb 17, 2023

It's not possible to subclass and override class methods from a Rust crate / JNI unfortunately, no.

The developer for the application can subclass the Activity (Either subclass NativeActivity or GameActivity) and then hook into onRequestPermissions there, and potentially call some API that your crate would document as a requirement.

Glad to see someone looking at permissions here though - I was recently starting to poke at this for Android too but haven't found much time to look at it yet still.

I think in this case it could be good to take a look at the currently recommended API for requesting permissions which isn't the original Activity.requestPermissions but rather there is an androidX API: https://developer.android.com/training/permissions/requesting#request-permission

That API doesn't actually require you to override onRequestPermissions, but it does require the app to be based on AppCompatActivity (Which is the case with GameActivity)

Alternatively you can subclass android.app.Fragment and implement onRequestPermissionsResult for the Fragment subclass and use Fragment::requestPermissions. This would enable the crate to work without needing to modify the Activity I believe but it will require a small amount of support code written in Java/Kotlin.

When I started looking at this recently I was initially looking at that last approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants