onRequestPermissionsResult from v4.Fragment Lollipop
<p>I have a nested Fragment inside an Activity where I am trying to request Permission for ACCESS_FINE_LOCATION &amp; ACCESS_COARSE_LOCATION.</p>

<p>In Android.M it works fine callbacks in the same Fragment but in lollipop the callback comes but the request codes are empty.</p>

<p>This is my Fragment inside the MainActivity</p>

<pre class="lang-java prettyprint-override"><code>private static final int MY_LOC_PERMISSIONS = 123;
private static GoogleMap mMap;
private LocationManager locationManager;

private void setUpMap() {
    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            //Tried this from Fragment 
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},MY_LOC_PERMISSIONS);
            //Tried this from Activity -&gt; calls to Activity.onRequestPermissionsResult
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_LOC_PERMISSIONS);
    }else{
        locationManager.removeUpdates(this);
    }
}
</code></pre>

<p>and the request Callback in the same Fragment</p>

<pre class="lang-java prettyprint-override"><code>@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case MY_LOC_PERMISSIONS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length &gt; 0 &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                setUpMap();
            } else {
                // permission denied, boo! Disable the functionality that depends on this permission.
            }
            return;
        }
        // other 'case' lines to check for other permissions this app might request
    }
}
</code></pre>

<p>I also tried this from the activity callback but same issue endless loop since request codes are null when it is received in the Fragment from SO post <a href="https://stackoverflow.com/questions/32890702/request-runtime-permissions-from-v4-fragment-and-have-callback-go-to-fragment">Request runtime permissions from v4.Fragment and have callback go to Fragment?</a></p>



<pre><code>@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    List&lt;Fragment&gt; fragments = getSupportFragmentManager().getFragments();
    if (fragments != null) {
        for (Fragment fragment : fragments) {
            fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}
</code></pre>

<p>I tried the same thing from an activity it works like a charm. But I need this in the fragment. Also I created a function where I can check for functions in my Activity but when I am trying to do the same in the Fragment it doesn't even let me compile, like in Activity</p>



<pre><code>private boolean isRequestRequired() {
if (Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.M) {
    return true;
}
if (checkSelfPermission(Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED) {
    return true;
}
if (shouldShowRequestPermissionRationale(Manifest.permission.SEND_SMS)) {
    Snackbar.make(findViewById(R.id.fab), "Please allow required access", Snackbar.LENGTH_INDEFINITE)
            .setAction(android.R.string.ok, new View.OnClickListener() {
                @Override
                @TargetApi(Build.VERSION_CODES.M)
                public void onClick(View v) {
                    requestPermissions(new String[]{Manifest.permission.SEND_SMS}, MY_PERMISSIONS);
                }
            }).show();
} else {
    requestPermissions(new String[]{Manifest.permission.SEND_SMS}, MY_PERMISSIONS);
}
return false;
}
</code></pre>

<p>and in the activity I am checking like
</p>

<pre><code>if (!isRequestRequired()) {
    return;
}else{
  SmsManager sms = SmsManager.getDefault();
  sms.sendTextMessage(&lt;NUMBER&gt;,null,&lt;TEXT&gt;,null,null);
}
</code></pre>

<p>But the same thing in fragment in Android studio it errors out: "you have to check for request permission ..."</p>