0

I tested my application with a lot of logs and it seems that my application stuck at ActivityCompat.requestPermissions and it doesn't display any message. I think the problem might be getActivity() because I am in a Fragment.

@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    result = view.findViewById(R.id.tab3Result);
    progressBar = view.findViewById(R.id.tab3ProgressBar);
    view.findViewById(R.id.tab3GetLocation).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (ContextCompat.checkSelfPermission(getActivity().getApplicationContext(),
                    Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_DENIED) {
                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_LOCATION_PERMISSION);
            } else {
                getCurrentLocaiton();
            }
        }
    });
}
learner123
  • 165
  • 10
  • Check this link : https://stackoverflow.com/questions/40760625/how-to-check-permission-in-fragment – Tugay Sep 10 '20 at 22:19

1 Answers1

1

First I'd like to point out there is a typo: getCurrentLocaiton();

Other than that:

Could it be your context? I mean this is totally irrelevant if the issue is simply the typo so fix that first.

getApplicationContext() refers to the entire application context.
The alternative is getContext() which refers to the specific view that you're in.

I'm reading about it and found some perhaps useful information. https://developer.android.com/training/permissions/requesting //the part that you need to request in context.

Then I read this: Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

Since you're in a fragment, you may need to request permission inside the fragment.

Krausladen
  • 138
  • 10