Permission requests cause an infinite loop in OnResume

In API> = 23, we have to ask users for permission at runtime. But for some reason, the permissions cause onResume to be called indefinitely. What is causing this?

  @Override
protected void onResume() {
    super.onResume();

     ActivityCompat.requestPermissions(MainActivity.this,
     new String[]{Manifest.permission.ANYPERMISSION},1);       

}

 @Override
public void onRequestPermissionsResult(int requestCode,
         String permissions[], int[] grantResults) {
     }  

      

+3


source to share


3 answers


When you show the permission dialog, Acitvity goes to onPause

, and when the dialog is hidden, it goes to onResume

. You must change the location of the permit.



+9


source


Small piece of code for permissions to complete the previous answer :)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (Build.VERSION.SDK_INT >= 23)
        ensurePermissions(
                Manifest.permission.GET_ACCOUNTS,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        );
}

      



and

@TargetApi(23)
private void ensurePermissions(String... permissions) {
    boolean request = false;
    for (String permission : permissions)
        if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
            request = true;
            break;
        }

    if (request) {
        requestPermissions(permissions, REQUEST_CODE_PERMISSION);
    }
}

      

+1


source


your application must first check if a specific permission has been granted to you before asking for execute permission.

 if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
            android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
    } else {
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
    }

      

0


source







All Articles