Home:ALL Converter>Can we ask same runtime permission at the different location in android?

Can we ask same runtime permission at the different location in android?

Ask Time:2016-09-30T15:43:02         Author:aarav

Json Formatter

I have a navigation drawer. In navigation drawer I am doing inflate the fragment. At the first fragment I ask to user for the runtime permission. but in the second fragment I want to do that it ask user for the same permission again. but in my case first fragment ask the permission. but in the second fragment it does not ask for the permission. so can anyone tell me what is the issue? because I am using the same code in the both situation.

This is my first fragment code :-

if (checkPermission(Manifest.permission.ACCESS_FINE_LOCATION, getActivity().getApplicationContext(), getActivity())) {

                    IsConsumed = 1;
                    fetchLocationData();
                } else {

                    requestPermission(Manifest.permission.ACCESS_FINE_LOCATION, PERMISSION_REQUEST_CODE_LOCATION, getActivity().getApplicationContext(), getActivity());
                }


 public void requestPermission(String strPermission, int perCode, Context _c, Activity _a) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(_a, strPermission)) {


            Toast.makeText(getActivity(), "GPS permission allows us to access location data. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        } else {

            ActivityCompat.requestPermissions(_a, new String[]{strPermission}, perCode);
        }
    }


    public static boolean checkPermission(String strPermission, Context _c, Activity _a) {
        int result = ContextCompat.checkSelfPermission(_c, strPermission);
        if (result == PackageManager.PERMISSION_GRANTED) {

            return true;

        } else {

            return false;

        }
    }
 @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {

            case PERMISSION_REQUEST_CODE_LOCATION:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    fetchLocationData();

                } else {

                    Toast.makeText(getActivity(), "Permission Denied, You cannot access location data.", Toast.LENGTH_LONG).show();

                }
                break;

        }
    }

I am using the same code in the second fragment. but it does not ask for the permission.I tried to print the log it goes into the if() condition.and print the log. but it does not ask for the permission.

Author:aarav,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/39786230/can-we-ask-same-runtime-permission-at-the-different-location-in-android
yy