Alexei
Alexei

Reputation: 15646

Android 6.0+: Method onRequestPermissionsResult not call

Here my map fragment:

public class MapFragmentTab extends Fragment implements OnMapReadyCallback {
    private GoogleMap googleMap;
    private GoogleApiClient mGoogleApiClient;
    private static final int MY_PERMISSION_LOCATION = 1;   

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.map_fragment_tab, container, false);
        init();
        return rootView;
    }

    private void init() {
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                    .addApi(LocationServices.API)
                    .build();
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) 
        mapView = rootView.findViewById(R.id.mapView);
        if (mapView != null) {
            mapView.onCreate(null);
            mapView.onResume();
            mapView.getMapAsync(this);
        }
    }

    @Override
    public void onMapReady(GoogleMap map) {
        googleMap = map;       
        int permissionCheck = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_LOCATION);
        } else {                
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
         // some code here
        }
    }          

On Android 4.3 all work fine.

But when I start first time on Android 6.0+, then show prompt dialog for permission. Something like this:

Allow My application to access this device's location?

Deny Allow

I click Allow but the method onRequestPermissionsResult not call. What's wrong with my code?

Upvotes: 1

Views: 59

Answers (2)

Sharath kumar
Sharath kumar

Reputation: 4132

Instead of calling

ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_LOCATION)

call like this

requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_LOCATION, PERMISSIONS_CODE)

When you are using android.support.v4.app.Fragment, you have to call requestPermissions.

When in AppCompatActivity, you should use ActivityCompat.requestPermissions;

Upvotes: 1

Manoj Perumarath
Manoj Perumarath

Reputation: 10194

Permission result callback won't get triggered in fragment. For this to happen you should implement onRequestPermissionsResult in activity and transfer the callback to fragment.

@Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] 
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
List<Fragment> fragments = getSupportFragmentManager().getFragments();
if (fragments != null) {
    for (Fragment fragment : fragments) {
        fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
 }

Upvotes: 0

Related Questions