Kotlin – How to reference this.context in a fragment?

I am implementing fragments to an app in android studio. In my Home fragment, I have a function which requires this.context or main activity context.

class Home : Fragment() {

    private lateinit var fusedLocationProviderClient: FusedLocationProviderClient
    private lateinit var fragmentHomeBinding: FragmentHomeBinding



    @SuppressLint("WrongViewCast")
    override fun onCreateView(



        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?): View? {
        fragmentHomeBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, true)
        getCurrentLocation()
        fragmentHomeBinding.etGetCityName.setOnEditorActionListener { v, actionId, keyEvent ->
            if(actionId == EditorInfo.IME_ACTION_SEARCH)
            {
                getCityWeather(fragmentHomeBinding.etGetCityName.text.toString())
                val view = activity?.currentFocus
                if(view!=null)
                {
                    val imm:InputMethodManager=
                        activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    imm.hideSoftInputFromWindow(view.windowToken, 0)
                    fragmentHomeBinding.etGetCityName.clearFocus()
                }
                true
            }
            else false
        }



        // Inflate the layout for this fragment
        return fragmentHomeBinding.root
    }
    private fun getCityWeather(cityName:String)
    {
        fragmentHomeBinding.pbLoading.visibility = View.VISIBLE
        ApiUtilities.getApiInterface()?.getCityWeatherData(cityName, API_KEY)?.enqueue(
            object:Callback<ModelClass>{
                override fun onResponse(call: Call<ModelClass>, response: Response<ModelClass>) {
                    setDataOnViews((response.body()))
                }

                override fun onFailure(call: Call<ModelClass>, t: Throwable) {
                    Toast.makeText(activity?.applicationContext, "Wrong City", Toast.LENGTH_SHORT).show()
                }

            })
    }

    private fun getCurrentLocation()
    {
        if(checkPermission())
        {
            if(isLocationEnabled())
            {
                if(ActivityCompat.checkSelfPermission(
                        requireActivity(),android.Manifest.permission.ACCESS_FINE_LOCATION
                    ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                        requireActivity(),
                        android.Manifest.permission.ACCESS_COARSE_LOCATION
                    ) != PackageManager.PERMISSION_GRANTED
                ){
                    requestPermission()
                    return
                }
                fusedLocationProviderClient.lastLocation.addOnCompleteListener(requireActivity()){ task->
                    val location: Location?=task.result
                    if(location==null)
                    {
                        fetchCurrentLocationWeather(23.76137119142536.toString(), 90.35059989467042.toString())
                    }
                    else
                    {
                        fetchCurrentLocationWeather(location.latitude.toString(),location.longitude.toString())
                    }
                }

            }
            else
            {
                Toast.makeText(activity?.applicationContext, "Turn on Location", Toast.LENGTH_SHORT).show()
                val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                startActivity(intent)
            }
        }
    }

    private fun fetchCurrentLocationWeather(latitude: String, longitude:String)
    {
        fragmentHomeBinding.pbLoading.visibility = View.VISIBLE
        ApiUtilities.getApiInterface()?.getCurrentWeatherData(latitude, longitude, API_KEY)?.enqueue(
            object : Callback<ModelClass> {
                override fun onResponse(call: Call<ModelClass>, response: Response<ModelClass>) {
                    if(response.isSuccessful)
                    {
                        setDataOnViews(response.body())
                    }
                }

                override fun onFailure(call: Call<ModelClass>, t: Throwable) {
                    Toast.makeText(activity?.applicationContext, "ERROR", Toast.LENGTH_SHORT).show()
                }

            })
    }

    private fun setDataOnViews(body: ModelClass?) {
        val sdf = SimpleDateFormat("dd/MM/yyyy hh:mm")
        val currentDate = sdf.format(Date())
        fragmentHomeBinding.tvDateTime.text = currentDate
        fragmentHomeBinding.tvDayMaxTemp.text = ""+kelvinToCelsius(body!!.main.temp_max) + "°"
        fragmentHomeBinding.tvDayMinTemp.text = ""+kelvinToCelsius(body!!.main.temp_min) + "°"
        fragmentHomeBinding.tvTemp.text = ""+kelvinToCelsius(body!!.main.temp)
        fragmentHomeBinding.tvFeelsLike.text = "Feels like: "+kelvinToCelsius(body!!.main.feels_like) + "°"
        fragmentHomeBinding.tvWeatherType.text = body.weather[0].main
        fragmentHomeBinding.tvPressure.text = body.main.pressure.toString()
        fragmentHomeBinding.tvHumidity.text = body.main.humidity.toString() + " %"
        fragmentHomeBinding.tvWindSpeed.text = body.wind.speed.toString() + " m/s"
        fragmentHomeBinding.tvCityName.text = body.name
        updateUI(body.weather[0].id)
    }


    private fun updateUI(id: Int) {
        fragmentHomeBinding.pbLoading.visibility = View.GONE
        fragmentHomeBinding.mainLayout.visibility = View.VISIBLE
    }

    private fun kelvinToCelsius(temp: Double): Double
    {
    var intTemp = temp
        intTemp = intTemp.minus(273)
        return intTemp.toBigDecimal().setScale(1, RoundingMode.UP).toDouble()
    }


    companion object
    {
        const val PERMISSION_REQUEST_ACCESS_LOCATION = 100
        const val API_KEY = "3c709ccaf2730aa1c263925f75db631a"
    }

    private fun isLocationEnabled():Boolean{
        val locationManager:LocationManager=activity?.getSystemService(Context.LOCATION_SERVICE) as LocationManager
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(
            LocationManager.NETWORK_PROVIDER
        )
    }

    private fun requestPermission(){
        ActivityCompat.requestPermissions(
            requireActivity(), arrayOf(android.Manifest.permission.ACCESS_COARSE_LOCATION,
            android.Manifest.permission.ACCESS_FINE_LOCATION),
            PERMISSION_REQUEST_ACCESS_LOCATION
        )
    }

    private fun checkPermission():Boolean{
        if(ActivityCompat.checkSelfPermission(requireActivity(),
            android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(requireActivity(),
            android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        {
            return true
        }
        return false
    }

    @Suppress("DEPRECATION")
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if(requestCode== PERMISSION_REQUEST_ACCESS_LOCATION)
        {
            if(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                Toast.makeText(activity?.applicationContext, "Granted", Toast.LENGTH_SHORT).show()
                getCurrentLocation()
            }
            else{
                Toast.makeText(activity?.applicationContext, "Denied", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

When I set the attachtoparent to true, I get an error saying the child already has a parent. In the function getCurrentLocation()I used requireActivity() instead of this.context.

Help’s appreciated, thanks!

Logcat:

2022-05-20 23:08:18.723 10736-10736/com.example.weather E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.weather, PID: 10736
    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:5247)
        at android.view.ViewGroup.addView(ViewGroup.java:5076)
        at android.view.ViewGroup.addView(ViewGroup.java:5016)
        at androidx.fragment.app.FragmentStateManager.addViewToContainer(FragmentStateManager.java:833)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1455)
        at android.app.Activity.performStart(Activity.java:8076)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3660)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
2022-05-20 23:08:18.739 10736-10736/com.example.weather I/Process: Sending signal. PID: 10736 SIG: 9

Leave a Comment