0

I have a dialog that loads tabLayout with 5 sections and its content are loaded using ViewPager with View Pager adapter. Now, in one of the sections, I have to load images from the gallery. I have written the runtime permission, but I cannot use the callback of the permission as I have implemented this dialog in a class that doesn't have an activity. I have no idea how to implement the callback method of permissions in an activity-less class.

public void makePermissionRequest(){ if(ContextCompat.checkSelfPermission(showEditorActivity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(showEditorActivity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, permission_request_code); }else{ fetchImagesFromDevice(); } }

and for callback..

 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case permission_request_code: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.i("permission..link", "allowed");
                fetchImagesFromDevice();

            }else{
                Log.i("permission..link", "denied");
                if (ContextCompat.checkSelfPermission(showEditorActivity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    if (ActivityCompat.shouldShowRequestPermissionRationale(showEditorActivity, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                        showMessageOKCancel("You need to allow access to your Internal Memory", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //log to find out answer..
                                if (which == -1) {
                                    makePermissionRequest();
                                }
                            }
                        });
                    }

                }
            }

        }

    }
}
AbiDoll
  • 106
  • 5
  • if you are calling from fragment i mean other than activity than your callback is in parent activity of fragment. – Ajay Pandya Mar 29 '16 at 09:52
  • yes, since the callback is from the parent activity, my whole app restarts if the user chooses deny. how can i redirect my callback method to the fragment? – AbiDoll Mar 29 '16 at 09:54
  • for deny you can set flag inside parent activity if its deny than you can directly show fragment which is previously showing check this for little bug http://stackoverflow.com/a/33170531/3514144 – Ajay Pandya Mar 29 '16 at 09:58
  • the fetchImagesFromDevice() method is called in the callback method. How do i trigger that? – AbiDoll Mar 29 '16 at 10:08
  • ok what i did is controlled the callback method from the parent activity and invoked fetchImagesFromDevice() method when permission is granted – AbiDoll Mar 29 '16 at 11:07

0 Answers0