-4

Below code is working fine in Activity but I cannot make it work inside a Fragment.

The onRequestPermissionsResult is never gets called.

There is a requestPermissions function in API level 23 (Android 6.0) but I need to make it work on lower levels like API level 21 (Android 5.0) so unfortunetely I cannot use that. ActivityCompat.requestPermissions just simply not calls onRequestPermissionsResult.

Any suggestion?

public class MyFragment extends Fragment {

final int REQUEST_CODE = 120;

@Override
public View onCreateView(LayoutInflater infl, ViewGroup cont, Bundle bundle) {

    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        Log.i("LOG", "Asking for permission right now..");

        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CODE);


        //This is working but needs API level 23 (Android 6.0) - How to make this work on Android 5.0?
        //requestPermissions( new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CODE);
    }

    return super.onCreateView(infl, cont, bundle);
}

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

    Log.i("LOG", "onRequestPermissionsResult!");

    if (requestCode == REQUEST_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Log.i("LOG", "GRANTED");
            Toast.makeText(getActivity(), "GRANTED,", Toast.LENGTH_LONG).show();

        } else {
            Log.i("LOG", "REFUSED");
            Toast.makeText(getActivity(), "REFUSED,", Toast.LENGTH_LONG).show();
        }
    }
}
}
Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222
  • `onCreateView` is a terrible choice for calling `requestPermissions` move it into `onResume` – Selvin Apr 25 '17 at 14:23
  • @Selvin I do not call it in production code. It is just a small dummy code that can show and reproduce easily the error. – Adam Varhegyi Apr 25 '17 at 14:24
  • Sorry but i dont understant well your idea, if your android device is on 5.0, so why you need to request the permission?? – Phạm Lam Apr 25 '17 at 14:24
  • @PhạmLam It is 5.0+ – Adam Varhegyi Apr 25 '17 at 14:25
  • also you should call Fragment's method not static ActivityCompat's method (which is written in similar question here on SO - **which means that you didn't even tried to do some internet search on this topic** ) – Selvin Apr 25 '17 at 14:26
  • 1
    Replace `ActivityCompat` with `FragmentCompat` and call its `requestPermissions()` method. As it stands, `onRequestPermissionsResult()` is being called... on the activity that you are passing to `ActivityCompat.requestPermissions()`. – CommonsWare Apr 25 '17 at 14:31

2 Answers2

0

Call fragment.onRequestPermissionResult inside the onRequestPermissionResult of the hosting activity.

Roudi
  • 1,249
  • 2
  • 12
  • 26
0

Requesting permissions in runtime was added in API 23. So it will not work in older versions. In API older than 23 permissions were granted at install time.

Edit: before asking for permission just call checkSelfPermission and call your code if already granted instead of asking for permission.

3mpty
  • 1,354
  • 8
  • 16