I want to implement new marshmallow support in application by giving the permission model to my app. Now I have given the below permission in my manifest file.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Now in an activity I have loaded a fragment in which I am having an recyclerview and data is loaded in this recyclerview using recyclerview adapter.
In this recyclerview adapter's list, I ma having a button clicking on which I would require the above permission. So, I have called the below function when the user will click on this button.
public void checkForManifestPermission(){
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(mContext,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity)mContext,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// 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.
ActivityCompat.requestPermissions((Activity)mContext,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
ConstantVariables.READ_EXTERNAL_STORAGE);
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions((Activity)mContext,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
ConstantVariables.READ_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
Now in the activity I have overridden the below callback method (from http://web.archive.org/web/20160111030837/http://developer.android.com/training/permissions/requesting.html).
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case ConstantVariables.READ_EXTERNAL_STORAGE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE = true;
} else {
ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE = false;
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
In the adapter where I am calling function checkForManifestPermission and checked the condition
if(ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE){}
Then only redirect to other activity.
Now my problem is that the code written inside this condition get executed before calling the onRequestPermissionsResult method, I want that if permission granted by user then only it should redirect to other activity.
How can I achieve this, I want my code to wait for the user to give response about approve or deny the permission and then proceed further accordingly.
Thanks a lot for the help.