millinet millinet - 1 month ago 15
Android Question

Android - PermissionHelper - Shown twice

I created a small permission helper and for some unknown reason, when I refuse the permission, I get it twice. The first one is normal, upon landing on the activity that requests the permission. The second one is shown when the permission is refused just after being redirected to the previous activity.

Thank you for any help,

The call to the helper in made in the onResume method, like this:

@Override
protected void onResume() {
super.onResume();
if(permissionHelper.checkPermission(this, Manifest.permission.READ_CONTACTS)){
initContact();
}
}


Then there is the onActivityPermissionResult which looks like this

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
permissionHelper.onRequestPermissionsResult(requestCode, grantResults, new PermissionHelper.PermissionListener() {
@Override
public void onPermissionGranted() {
initContact();
}

@Override
public void onPermissionDenied() {
Intent intent = new Intent(ContactActivity.this, MainActivity.class);
startActivity(intent);
}
});
}


My PermissionHelper looks like this:

public class PermissionHelper {

/**
* Two possible callback: granted or not.
*/
public interface PermissionListener {
void onPermissionGranted();
void onPermissionDenied();
}

private static PermissionHelper mInstance;
private static Activity mActivity;
private static String mPermission;
private static final int REQUEST_CODE = 1337;

private PermissionHelper() {
setmActivity(mActivity);
setmPermission(mPermission);
}

public static PermissionHelper getInstance(){
if(mInstance == null){
mInstance = new PermissionHelper();
}
return mInstance;
}

public static void setmActivity(Activity mActivity) {
PermissionHelper.mActivity = mActivity;
}

public static void setmPermission(String mPermission) {
PermissionHelper.mPermission = mPermission;
}

public static boolean checkPermission(Activity activity, String permission){

mActivity = activity;
mPermission = permission;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int perm = mActivity.checkSelfPermission(mPermission);
if (perm != PackageManager.PERMISSION_GRANTED) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mActivity.requestPermissions(new String[]{mPermission}, REQUEST_CODE);
return false;
}
}
}

return true;
}

/**
* Must be called from Activity's onRequestPermissionsResult()
*/
public static void onRequestPermissionsResult(int requestCode,
int[] grantResults,
PermissionListener permissionListener
) {
switch (requestCode) {
case REQUEST_CODE: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
permissionListener.onPermissionGranted();
}
else{
permissionListener.onPermissionDenied();
}
break;
}
}
}

}

Answer

So I finally had time to fix it. I made a first call to the checkPermission within the onResume and that was bad. onResume is also called after the onRequestPermissionsResult. So I moved the first call into onCreate.