title: Android permission automatically denied on requestPermission from MainActivity
body: <p>In the main activity of an Android application I check for permissions (<code>Manifest.permission.MANAGE_DOCUMENTS</code>), detect I do not have them, and call <code>requestPermisions</code>.
  Then in <code>onRequestPermissionResult</code> I almost immediately get denied permission, without a dialog ever showing.</p>
  
  <p>I already <em>confirmed</em> the same permission in another activity of the same app (through <code>requestPermissions</code> again, which works), so I expected this decision to be stored (for the session, or whatever), and I have never selected to deny the permission. Either way, the permission dialog is not displayed and the permission is denied automatically.</p>
  
  <p>So far I have tested this on emulators of Android 7 and 6 (API 24 and 23).</p>
  
  <p>I have tried:</p>
  
  <ul>
  <li>clearing the app's data cache and wiping the device, so it's fresh</li>
  <li>definitely not this <a href="https://stackoverflow.com/questions/38547480/getting-permission-denied-on-android-m">Getting permission denied on Android M</a></li>
  <li>triple-checked spelling after seeing this <a href="https://stackoverflow.com/questions/35633110/onrequestpermissionsresult-returns-immediately-with-denied-permission">onRequestPermissionsResult returns immediately with denied permission</a> and I am calling the super method</li>
  <li>a bunch of other suggestions on stack overflow</li>
  </ul>
  
  <p>I'm pretty stumped...</p>
  
  <p>Here is the permission request (see comment in the code):</p>
  
  <pre><code>private fun askForPermissionOrSendRequest(view: View, permission: String) {
      if (checkSelfPermission(permission) == PackageManager.PERMISSION_DENIED) {
          if (shouldShowRequestPermissionRationale(permission)) {
              cachedView = view
              val explanationDialog = AlertDialog.Builder(this).setMessage("We need permissions to read your storage in order to show your profile image.").setOnDismissListener {
                  requestPermissions(
                          arrayOf(permission),
                          BSMainActivity.permissionRequestSendProfilePic
                  )
              }.create()
              explanationDialog.show()
  
          } else {
              cachedView = view
              // this branch is always hit - the permission seems to be missing every time
              requestPermissions(
                      arrayOf(permission),
                      BSMainActivity.permissionRequestSendProfilePic
              )
          }
      } else {
          sendRequest(view)
      }
  }
  </code></pre>
  
  <p>I immediately get to the result handler without a dialog showing up to ask me for permissions. I may or may not have confirmed the same permission in another (child) activity of the same app (doesn't seem to make a difference).</p>
  
  <pre><code>override fun onRequestPermissionsResult(requestCode: Int,
                                          permissions: Array&lt;String&gt;,
                                          grantResults: IntArray
  ) {
      super.onRequestPermissionsResult(requestCode, permissions, grantResults)
  
      when (requestCode) {
          BSMainActivity.permissionRequestSendProfilePic -&gt; {
              // This gets hit, MANAGE_DOCUMENTS was denied
              if (permissions.contains(Manifest.permission.MANAGE_DOCUMENTS) &amp;&amp; grantResults[permissions.indexOf(Manifest.permission.MANAGE_DOCUMENTS)] == PackageManager.PERMISSION_DENIED) {
                  Log.w(logName, "Permission to open image was denied while sending a tag request: %s %s".format(
                          permissions.joinToString(",", "[", "]"),
                          grantResults.joinToString(",", "[", "]")
                  ))
              }
  
              // send request regardless of the result for now
              sendRequest(cachedView)
          }
      }
  }
  </code></pre>
  
  <p>In my manifest I have the following:</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="me.monomon.bs"&gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
      &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/&gt;
      &lt;uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/&gt;
  </code></pre>