ssk ssk - 1 month ago 12
Android Question

onRequestPermissionsResult is not called back in an android activity

I am trying to get the camera permission from the user before launching my camera activity. onRequestPermissionsResult is never called back after the user presses "Allow" from the permission dialog. Here is my Activity class:

public class ImageCaptureActivity extends AppCompatActivity {

public static final String TAG = ImageCaptureActivity.class.getSimpleName();

private static final int REQUEST_CAMERA = 0;
private static final int REQUEST_CAMERA_PERMISSION = 1;
private Point mSize;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_capture);

Display display = getWindowManager().getDefaultDisplay();
mSize = new Point();
display.getSize(mSize);

// Request for Camera Permission
requestForCameraPermission(findViewById(android.R.id.content));
}

/**
* @param view
* @brief requestForCameraPermission
*/
public void requestForCameraPermission(View view) {
Log.v(TAG, "Requesting Camera Permission");

final String permission = Manifest.permission.CAMERA;
if (ContextCompat.checkSelfPermission(ImageCaptureActivity.this, permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(ImageCaptureActivity.this, permission)) {
showPermissionRationaleDialog(getString(R.string.camera_permission_rationale), permission);
} else {
requestForPermission(permission);
}
} else {
launch();
}
}

/**
* @param message
* @param permission
* @brief showPermissionRationaleDialog
*/
private void showPermissionRationaleDialog(final String message, final String permission) {
new AlertDialog.Builder(ImageCaptureActivity.this)
.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ImageCaptureActivity.this.requestForPermission(permission);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
})
.create()
.show();
}

/**
* @param permission
* @brief requestForPermission
*/
private void requestForPermission(final String permission) {
ActivityCompat.requestPermissions(ImageCaptureActivity.this, new String[]{permission}, REQUEST_CAMERA_PERMISSION);
}

/**
* @brief launch
*/
private void launch() {
Log.v(TAG, "Camera Permission Granted, launching the CameraActivity");
String documentId = getIntent().getStringExtra(IntentNames.INTENT_EXTRA_WIP_DOCUMENT_ID);
Intent startCustomCameraIntent = new Intent(this, CameraActivity.class);
startCustomCameraIntent.putExtra(IntentNames.INTENT_EXTRA_WIP_DOCUMENT_ID, documentId);
startActivityForResult(startCustomCameraIntent, REQUEST_CAMERA);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CAMERA_PERMISSION:
final int numOfRequest = grantResults.length;
final boolean isGranted = numOfRequest == 1
&& PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1];
Log.v(TAG, "Camera Permission callback on onRequestPermissionsResult");
if (isGranted) {
launch();
}
break;

default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}


Here is my App Manifest with the Camera Permission:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp.testpackage">

<uses-permission android:name="android.permission.CAMERA" />

<uses-feature
android:name="android.hardware.camera.front"
android:required="true" />
<uses-feature android:name="android.hardware.camera2" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application


My Gradle Dependency:

compile 'com.android.support:appcompat-v7:24.0.0'

compile 'com.android.support:design:24.0.0'

compile 'com.android.support:support-v4:24.0.0'

Answer

I tried your code myself and everything works just fine. I think that the only thing that can be a problem is the scenario when your activity has these two flags:

 android:noHistory="true"
 android:excludeFromRecents="true"

Here's a guy that is explains the issue: http://stackoverflow.com/a/35772265/5281581