I have a fragment in which I have recyclerview and setting data in this recyclerview using recyclerview adapter.
Now, I am having a button in the adapter\'s list ite
This is a common mistake that people make when coding for marshmallow.
When in AppCompatActivity, you should use ActivityCompat.requestPermissions; When in android.support.v4.app.Fragment, you should use simply requestPermissions (this is an instance method of android.support.v4.app.Fragment) If you call ActivityCompat.requestPermissions in a fragment, the onRequestPermissionsResult callback is called on the activity and not the fragment.
requestPermissions(permissions, PERMISSIONS_CODE);
If you are calling this code from a fragment it has it’s own requestPermissions method.
So basic concept is, If you are in an Activity, then call
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
and if in a Fragment, just call
requestPermissions(new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
For the reference, I have obtained the answer from this link https://www.coderzheaven.com/2016/10/12/onrequestpermissionsresult-not-called-on-fragments/
private void showContacts() {
if (getActivity().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSIONS_REQUEST_READ_STORAGE);
} else {
doShowContacts();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_STORAGE
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
doShowContacts();
}
}
change permission
I had the same issue. Your fragment can be initialized from the activity layout. Like that:
main_activty.xml
<fragment
android:id="@+id/fragment"
android:name="com.exampe.SomeFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
This issue was solved for me when I used FragmentTransaction
instead