0

I've a activity which has onRequestPermissionsResult which is called whenever I request for permission at runtime but when I replace fragment from another fragment it is not called.

The process is: I have 3 Fragment Fragment A,B and C. Fragment A is the default fragment attached to MainActivity and on click of button I replace Fragment A to Fragment B and when I request permission for Fragment B it is running properly but after replacing Fragment B to Fragment C requesting a permission throws exception fragment not attached to activity

MainActivity Code:

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

                fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

Fragment B Code

Fragment fragment = new ShedDetailFragment();
        Bundle args=new Bundle();
        args.putString("flockno",flockno);
        args.putString("shedno",shedno.get(position));
        fragment.setArguments(args);
        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .add(fragment,"ShedDetailsFrament")
                .replace(R.id.frame_container, fragment)
                .commit();

Fragment C Code

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

    switch (requestCode) {
        case 12:{
            // 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.
                takePhotoFromCamera();
                //Toast.makeText(getActivity(),"Permission Granted for Camera.Please Select Farm Photo",Toast.LENGTH_SHORT).show();

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }
        case 11:{
            // 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.
                saveImage(bitmap);
                //Toast.makeText(getActivity(),"Permission Granted for Storage.Please Select Farm Photo",Toast.LENGTH_SHORT).show();

            } 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
    }
}

Error Log When I Allow Any Permission

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.artevet.artevet, PID: 32741
              java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=12, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.artevet.artevet/com.artevet.artevet.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.onRequestPermissionsResult(int, java.lang.String[], int[])' on a null object reference
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4998)
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041)
                  at android.app.ActivityThread.access$1600(ActivityThread.java:229)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:7325)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.onRequestPermissionsResult(int, java.lang.String[], int[])' on a null object reference
                  at com.artevet.artevet.MainActivity.onRequestPermissionsResult(MainActivity.java:383)
                  at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7291)
                  at android.app.Activity.dispatchActivityResult(Activity.java:7169)
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4994)
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041) 
                  at android.app.ActivityThread.access$1600(ActivityThread.java:229) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:7325) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
bhavikshah28
  • 98
  • 1
  • 9

1 Answers1

1

Is there any specific reason why you request permissions from your activity? You can just handle permissions in each of your fragment. Because the fragment has own requestPermissions() and onRequestPermissionsResult methods. For example see this

Fatih Santalu
  • 4,641
  • 2
  • 17
  • 34