Challenge: Permissions Rationale

Since no one has yet proposed a solution, I’ll publish mine.
I’ve created the inner class in FragmentLocation:

        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            if (getActivity() != null) {
                return new AlertDialog.Builder(getActivity())
                        .setMessage("Locatr uses location data to find images near you on Flickr")
                        .setPositiveButton("Got it", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dismiss();
                            }
                        })
                        .create();
            } else {
                return super.onCreateDialog(savedInstanceState);
            }
        }

        @Override
        public void onDismiss(DialogInterface dialog) {
            super.onDismiss(dialog);
            requestPermissions(LOCATION_PERMISSIONS, REQUEST_LOCATION_PERMISSIONS);
        }
    }

Also I have changed onOptionsItemSelected() method:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_locate:
                if (hasLocationPermission()) {
                    findImage();
                } else {
                    if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
                        FragmentManager fm = getFragmentManager();
                        if (fm != null) {
                            new PermissionRationaleDialog().show(getFragmentManager(), "PermissionRationaleDialog");
                        }
                    } else {
                        requestPermissions(LOCATION_PERMISSIONS, REQUEST_LOCATION_PERMISSIONS);
                    }
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

And that’s all. )
Compare it with your solution.

1 Like

The inner class looks should start with

public static class PermissionRationaleDialog extends DialogFragment {

    @NonNull
    @Override
    ...............

Your solution is too complicated. It could be solved simpler:

LocatrFragment.java:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_locate:
            if (hasLocationPermission()) {
                findImage();
            } else if (shouldShowRequestPermissionRationale(LOCATION_PERMISSIONS[0])) {
                Log.i(TAG, "onOptionsItemSelected: Should");
                AlertDialog dialog = new AlertDialog.Builder(getActivity()).create();
                dialog.setMessage(getString(R.string.explaining));
                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        Log.i(TAG, "onDismiss: listen");
                        requestPermissions(LOCATION_PERMISSIONS, REQUEST_LOCATION_PERMISSIONS);
                    }
                });
                dialog.show();
            } else {
                Toast.makeText(getActivity(), R.string.explaining, Toast.LENGTH_SHORT).show();
                requestPermissions(LOCATION_PERMISSIONS, REQUEST_LOCATION_PERMISSIONS);
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

strings.xml

<string name="explaining">Locatr uses location data to find images near you on Flickr</string>

1 Like