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.