0

In my project I request marshmallow permissions in fragment after adding permissions onrequestpermissiomResult is not calling in my fragment my code:

public class  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    profileView = inflater.inflate(R.layout.fragment_profile, container, false);
    server_utilities = new ServerUtilities();
    utilities = new Utilities(getActivity());
    getUserProfileInfo();
    callbackManager = CallbackManager.Factory.create(); 

cameraImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ***if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //First checking if the app is already having the permission
                if (utilities.isReadStorageAllowed(getActivity())) {
                    //If permission is already having then showing the toast
                    Toast.makeText(ctx.getApplicationContext(), "You already have the permission", Toast.LENGTH_LONG).show();
                    //Existing the method with return
                    showFourButtonsBSDialog();
                } else {
                    //If the app has not the permission then ask for the permission
                    utilities.checkPermissions(ctx, getActivity());
                }
            } else
                showFourButtonsBSDialog();***
        }
    });

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    switch (requestCode) {
        case REQUEST_PICKIMAGE:
            //If permission is granted
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                //Displaying a toast
                Toast.makeText(MainActivity.this, "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
                profile_fragment.showFourButtonsBSDialog(); //todo
            } else {
                //Displaying another toast if permission is not granted
                Toast.makeText(MainActivity.this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
            }
    }
}

How to add marshmallow permissions in fragment of an activity?

James Z
  • 12,209
  • 10
  • 24
  • 44
uma
  • 259
  • 3
  • 22

2 Answers2

1

This Code works for me on all API levels >= 23

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (getContext().checkSelfPermission(
                            Manifest.permission.READ_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED) {
                        requestPermissions(
                                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                                FINE_GALLERY_PERMISSION_REQUEST);
                    } else {
                        galleryIntent();
                    }
                }
                else{
                    galleryIntent();
                }

and in your method dont forget to add

startActivityForResult(yourIntent, YOUR_REQUEST_CODE);

Since you didnt post the method you are calling after permission i cant tell if you're sending the correct request code,anyway check that the request codes are the same, and dont forget to add the permission in the manifest!

Ahmad Sabeh
  • 526
  • 5
  • 18
  • is this in the switch case in the onRequestPermissionResult : if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { //Toast.makeText(getContext(), "PERMISSION GRANTED", Toast.LENGTH_SHORT).show(); cameraIntent(); } – Ahmad Sabeh Nov 10 '17 at 10:13
  • ya it is in switch case – uma Nov 10 '17 at 10:19
0

after one hour googled, By adding this to our parent activity

  private void replaceFragmentInternal(int contentFrameId, Fragment replacingFragment) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(contentFrameId, replacingFragment)
            .commit();
}

it will automatically call onrequest permission result in related fragments for reference, please have a look at this Request runtime permissions from v4.Fragment and have callback go to Fragment?

uma
  • 259
  • 3
  • 22