简体   繁体   中英

ForYouFragment Fragment doesn't update

Hi i have permission check on my forYou fragment when the app starts start and you give the permission and it makes a api call and get the weather for you current location the problem is I must switch the fragment for getting the call and switch again for showing it in the reclyerview the problem is when I give the permission for the location it doesn't do the api after that, I must refresh the fragment

    fusedLocationClient.lastLocation.addOnSuccessListener(requireActivity()) { location ->
        if (location != null) {
            currentLocation = location
            val currentLatLng = LatLng(location.latitude, location.longitude)
            val currentAddress = getAddress(location.latitude, location.longitude)
            Log.d("TAG", "klagenfurt : ${currentAddress}")
            retrofitOneCallResponse(location.latitude, location.longitude, currentAddress)
            dataService.getFavorites(this)

        }


    }
    if (ActivityCompat.checkSelfPermission(
            requireContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        ActivityCompat.requestPermissions(
            requireActivity(),
            arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
            MapViewFragment.LOCATION_PERMISSION_REQUEST_CODE
        )

        return
    }

Create one method like this.

private fun getLocation(){
fusedLocationClient.lastLocation.addOnSuccessListener(requireActivity()) { location ->
        if (location != null) {
            currentLocation = location
            val currentLatLng = LatLng(location.latitude, location.longitude)
            val currentAddress = getAddress(location.latitude, location.longitude)
            Log.d("TAG", "klagenfurt : ${currentAddress}")
            retrofitOneCallResponse(location.latitude, location.longitude, currentAddress)
            dataService.getFavorites(this)
        }else{
         getLocation()
        }
    }
}

then from here

if (ActivityCompat.checkSelfPermission(
            requireContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        requestPermissions(
            arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
            MapViewFragment.LOCATION_PERMISSION_REQUEST_CODE
        )
    }else{
      getLocation()
}


override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray): Unit {
      if(requestCode==MapViewFragment.LOCATION_PERMISSION_REQUEST_CODE){
          if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
              getLocation();
          }
      }
}

Also, override the onRequestPermissionsResult in the fragment. and check if permission granted the call getLocation() .

enter image description here HERE it says and you write the code without ActivityCompat on the requestpermisson line it doesn't work etiher

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM