title: When to request permission at runtime for Android Marshmallow 6.0?
body: <p>I am testing my app on <code>Marshmallow 6.0</code> and it's getting force closed for the <code>android.permission.READ_EXTERNAL_STORAGE</code>, even if it is defined in the Manifest already. Somewhere I have read that if I request permission at runtime then it would not force close your application. I have read <a href="http://developer.android.com/intl/es/training/permissions/requesting.html" rel="nofollow noreferrer">this android document</a> also, which is for requesting runtime permission. </p>
  
  <p>So, I came to know that we can request a permission like below which is mentioned in the android document.</p>
  
  <pre><code>// Here, thisActivity is the current activity
  if (ContextCompat.checkSelfPermission(thisActivity,
                  Manifest.permission.READ_CONTACTS)
          != PackageManager.PERMISSION_GRANTED) {
  
      // Should we show an explanation?
      if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
              Manifest.permission.READ_CONTACTS)) {
  
          // Show an expanation to the user *asynchronously* -- don't block
          // this thread waiting for the user's response! After the user
          // sees the explanation, try again to request the permission.
  
      } else {
  
          // No explanation needed, we can request the permission.
  
          ActivityCompat.requestPermissions(thisActivity,
                  new String[]{Manifest.permission.READ_CONTACTS},
                  MY_PERMISSIONS_REQUEST_READ_CONTACTS);
  
          // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
          // app-defined int constant. The callback method gets the
          // result of the request.
      }
  }
  </code></pre>
  
  <p>The above code has a callback method <code>onRequestPermissionsResult</code> which gets the result.</p>
  
  <pre><code>@Override
  public void onRequestPermissionsResult(int requestCode,
          String permissions[], int[] grantResults) {
      switch (requestCode) {
  
       }
  }
  </code></pre>
  
  <p>My question is where to exactly request the permission to user? Should we use the requesting permission at start of the app or should we do it as when the permission is required?</p>