Hello I am still fairly new to this, so please help me understand why I am having this error. I have tried many solutions, so I'm just going to list everything I've done since I can't seem to understand why this is happening.
I created a project that integrates GoogleMaps at min SDK 21 to target/compile at SDK 28. I did call the permissions needed inside the Manifesto.
I created a file that extends the MapFragment class and everything seems to be working fine. I am able to check and request permission for the user's location (the box does show up), but when I called the onRequestPermissionResult method it is shown differently and gives me an error saying "error: cannot find symbol class NonNull":
@Override
public void onRequestPermissionsResult(int requestCode, @androidx.annotation.NonNull String[] permissions, @androidx.annotation.NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
In my other Fragment (android.support.v4.app) classes the @androidx.annotation.NonNull is @NonNull instead. I first thought maybe I needed to add implementation 'com.android.support:support-annotations:28.0.0' to the build.gradle, but that wasn't the case. I then tried to just replace the @androidx.annotation.NonNull with @NonNull which made the error go away, but whenever I clicked allow or deny it wasn't hitting the onRequestPermissionResult method.
I created a method that checks for a permission, but it won't let me use requestPermission on its own without checking if the build is greater or equal to SDK 23, but my min SDK is 21. So instead I just checked if the build is greater or equal to 21 and used ActivityCompat to get the requestPermission method and it works. It will check and ask for permission, so I'm thinking maybe the onRequestPermissionResult only works in the MainActivity which is what I don't want. I want to be able to call a method after checking if the request was granted inside the MapFragment. Is it because MapFragment isn't supported with android.support.v4.app? It looks like this:
private boolean checkAskPermission(int requestCode, String permissionString){
if(Build.VERSION.SDK_INT >= 21){
int permission = ContextCompat.checkSelfPermission(mContext, permissionString);
if(permission!=PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(getActivity(), new String[]{permissionString}, requestCode);
return false;
}
}
return true;
}
At this point I don't know what else to try. I thought maybe I wasn't checking the permission correctly inside onRequestPermissionResult when I change @androidx.annotation.NonNull to @NonNull to be able to use it, but the method doesn't hit when I use a break on it.
Please leave detail responses, so I can fully understand my problem. I have been stuck on this for a day.
Edit: Solution
private boolean checkAskPermission(int requestCode, String permissionString){
if(Build.VERSION.SDK_INT >= 23){
int permission = ContextCompat.checkSelfPermission(mContext, permissionString);
if(permission!=PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{permissionString}, requestCode);
return false;
}
}
return true;
}
and just changed the @androidx.annotation.NonNull to @NonNull and now it hits the method.
Thanks to Eugene for clearing up SDK permissions. Only SDK 23 and higher require permission.
import that annotation class:
import androidx.annotation.NonNull;
which is either coming from this dependency:
implementation "androidx.annotation:annotation:1.0.0"
or from this dependency, in case not yet using androidx
:
implementation "com.android.support:support-annotations:28.0.0"
then you can use it as usual:
@NonNull