Saturday, March 23, 2019

Android programming: request permissions in Activity vs Fragment


In Activity:

// check permission first 
if (ContextCompat.checkSelfPermission(this,
        Manifest.permission.WRITE_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED) {

    // request the permission 
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
else {
    // has the permission. 
    saveFile();
}


In Fragment:

// check permission first
if (ContextCompat.checkSelfPermission(getActivity(),
        Manifest.permission.WRITE_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED) {

    // request the permission
    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
else {
    // has the permission. 
    saveFile();
}


Both Activity and Fragment then override onRequestPermissionsResult() which has little difference as well.

In Activity:


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

    switch (requestCode) {
        case PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
            // If request is cancelled, the result arrays are empty. 
            if (grantResults.length > 0 
                   && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted. 
                saveFile();
            } else {
                // permission denied. 
                // tell the user the action is cancelled 
                AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                alertDialog.setMessage(getString(R.string.cannot_save_image));
                alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, getString(R.string.ok),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                alertDialog.show();
            }
            return;
        }
    }
}

In Fragment:

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

    switch (requestCode) {
        case PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
            // If request is cancelled, the result arrays are empty. 
            if (grantResults.length > 0 
                   && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted.
                saveFile();
            } else {
                // permission denied. 
                // tell the user the action is cancelled 
                AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
                alertDialog.setMessage(getString(R.string.cannot_save_image));
                alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, getString(R.string.ok),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                alertDialog.show();
            }
            return;
        }
    }
}


1 comment:

Anonymous said...

Make sure you add

in manifest file...

I had just spent 4 hours and then I realised this

 
Get This <