Allow fine location permission to replace fragment

后端 未结 1 1836
粉色の甜心
粉色の甜心 2021-01-27 06:14

I want to ask user does he want to grant a permission to accces his fine location and only if he grant it I want to replace a fragment and open the locator fragment. In locator

1条回答
  •  粉色の甜心
    2021-01-27 06:58

    Please check below solution, hope it will work properly for you.

     else if (id == R.id.nav_locator) 
     {
        int result = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
       if (result == PackageManager.PERMISSION_GRANTED){
    
           goToNextFragment();
    
       } else {
    
           requestForLocationPermission(); 
       }
     }
    
    
       private void requestForLocationPermission()
       {
    
          if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.ACCESS_FINE_LOCATION))
          {
          } 
          else {
    
              ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE);
          }
       }
    
      @Override
      public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) 
      {
          switch (requestCode) {
           case PERMISSION_REQUEST_CODE:
               if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
               goToNextFragment();
            } 
            break;
      }
    }
    
    
      public void goToNextFragment()
      {
          Fragment fragment = new LocatorFragment();
          FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
          ft.replace(R.id.fragmentlayout, fragment);
          ft.commit();
      }
    

    0 讨论(0)
提交回复
热议问题