I'm trying to implement Marshmallow's permission support, but my code inside "onRequestPermissionsResult" is never called. The "onCreate" and "onMapReady" parts of the code work fine. Checking if the device has the permission and requesting the permission works fine.
I'm using an Nexus 5X emulator running Android Marshmallow 6.0, API 23.
I have tried to do what it's mentioned here -> Android M Permissions: onRequestPermissionsResult() not being called
But I cannot get it to work, it doesn't matter if I use "ActivityCompat.requestPermissions" or directly "requestPermissions", it nevers get called.
I also updated my appcompat and support libraries to v24.2.0
Any ideas?
This is my activity:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
UiSettings mapSettings;
int MY_LOCATION_REQUEST_CODE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//Check location permission for sdk >= 23
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
// Request permission.
ActivityCompat.requestPermissions(MapsActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_LOCATION_REQUEST_CODE);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_LOCATION_REQUEST_CODE) {
if (permissions.length == 1 &&
permissions[0] == android.Manifest.permission.ACCESS_FINE_LOCATION &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
// Permission was denied. Display an error message.
}
}
}
}