Reputation: 1691
I'm using Google's EasyPermissions library. In my app I have two buttons, one to record video and one to capture image. Since both require Camera Permissions they are both annotated with @AfterPermissionGranted
.
So my method to record video looks like this :
@Override
@AfterPermissionGranted(RC_CAMERA_PERM)
public void openCameraToRecordVideo() {
if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Code here
}
And similarly for taking pictures :
@Override
@AfterPermissionGranted(RC_CAMERA_PERM)
public void openCameraToCaptureImage() {
if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Code here
}
They are both annotated with Permissions since I don't know which one user will click first.
What happens is when user clicks one button and accepts the permissions then both methods run one after the other. Which is obviously not the behavior I want.
I'd really appreciate any help on how to handle this situation. Thank you.
Upvotes: 4
Views: 638
Reputation: 563
Remove AfterPermissionGranted from both openCameraX function. Defined a private field lastAction. Write a new function openCamera with a AfterPermissionGranted annotation that check is lastAction is set and if so, call related function. In each openCameraX, check if you have camera permission and if not, update lastAction and start request camera permission.
Upvotes: 1
Reputation: 4517
Actually, what you are doing is asking the same permission two times with different function name so remove either openCameraToCaptureImage()
method or openCameraToRecordVideo()
method.
Upvotes: 1