Search code examples
androidkotlinandroid-fragmentsandroid-lifecyclelifecycleowner

Unable to access the Fragment View's LifecycleOwner when getView() is null i.e., before onCreateView() or after onDestroyView()


I've a fragment namely HomeFragment in my app. Following is the code for it:

import android.Manifest
import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.content.pm.PackageManager
import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import com.example.weatherapp.R
import com.example.weatherapp.WeatherApplication
import com.example.weatherapp.utils.GPS_REQUEST_CHECK_SETTINGS
import com.example.weatherapp.utils.GpsUtil
import com.example.weatherapp.utils.SharedPreferenceHelper
import com.example.weatherapp.utils.observeOnce
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.home_fragment.view.*

class HomeFragment : Fragment() {

    private lateinit var homeView: View
    private var isGPSEnabled = false
    private lateinit var prefs: SharedPreferenceHelper

    private val viewModel by viewModels<HomeViewModel> {
        HomeViewModel.HomeFragmentViewModelFactory(
            (requireContext().applicationContext as WeatherApplication).weatherRepository,
            requireActivity().application
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        prefs = SharedPreferenceHelper.getInstance(requireContext())
        GpsUtil(requireContext()).turnGPSOn(object : GpsUtil.OnGpsListener {
            override fun gpsStatus(isGPSEnabled: Boolean) {
                [email protected] = isGPSEnabled
            }
        })
    }
    override fun onStart() {
        super.onStart()
        invokeLocationAction()
    }

    private fun invokeLocationAction() {
        when {
            allPermissionsGranted() -> {
                viewModel.getLocationLiveData().observeOnce(
                    viewLifecycleOwner,
                    Observer { location ->
                        if (location != null) {
                            viewModel.getWeather(location)
                        }
                    }
                )
            }

            shouldShowRequestPermissionRationale() -> {
                AlertDialog.Builder(requireContext())
                    .setTitle("Location Permission")
                    .setMessage("This application requires access to your location to function!")
                    .setNegativeButton("No") { _, _ -> requireActivity().finish() }
                    .setPositiveButton("Ask me") { _, _ ->
                        requestPermissions(REQUIRED_PERMISSIONS, LOCATION_REQUEST_CODE)
                    }
                    .show()
            }

            !isGPSEnabled -> {
                Snackbar.make(
                    homeView.root,
                    "GPS is required for this application to function!",Snackbar.LENGTH_SHORT
                ).show()
            }

            else -> {
                requestPermissions(REQUIRED_PERMISSIONS, LOCATION_REQUEST_CODE)
            }
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?
    ): View? {
        homeView =  inflater.inflate(R.layout.home_fragment, container, false)
        return view;
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            Activity.RESULT_OK -> {
                when (requestCode) {
                    GPS_REQUEST_CHECK_SETTINGS -> {
                        isGPSEnabled = true
                        invokeLocationAction()
                    }
                }
            }

            Activity.RESULT_CANCELED -> {
                when (requestCode) {
                    GPS_REQUEST_CHECK_SETTINGS -> {
                        Snackbar.make(
                            homeView.root,"Enable your GPS and restart!",Snackbar.LENGTH_LONG
                        ).show()
                    }
                }
            }
        }
    }

    private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {
        ContextCompat.checkSelfPermission(requireContext(), it) == PackageManager.PERMISSION_GRANTED
    }

    private fun shouldShowRequestPermissionRationale() = REQUIRED_PERMISSIONS.all {
        shouldShowRequestPermissionRationale(it)
    }

    override fun onRequestPermissionsResult(
        requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == LOCATION_REQUEST_CODE) {
            invokeLocationAction()
        }
    }

    companion object {
        private val REQUIRED_PERMISSIONS = arrayOf(
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
        )
        private const val LOCATION_REQUEST_CODE = 123
    }
}

The app compiles successfully. But just as I open it on my device, the app crashes. And this is the error I'm getting in logcat upon running the app:

2021-05-25 17:45:25.968 32061-32061/com.example.weatherapp D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2021-05-25 17:45:25.989 32061-32061/com.example.weatherapp D/AndroidRuntime: Shutting down VM
    
    
    --------- beginning of crash
2021-05-25 17:45:25.997 32061-32061/com.example.weatherapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.weatherapp, PID: 32061
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.weatherapp/com.example.weatherapp.ui.MainActivity}: java.lang.IllegalStateException: Can't access the Fragment View's LifecycleOwner when getView() is null i.e., before onCreateView() or after onDestroyView()
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2949)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1745)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:200)
        at android.app.ActivityThread.main(ActivityThread.java:6956)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:519)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:836)
     Caused by: java.lang.IllegalStateException: Can't access the Fragment View's LifecycleOwner when getView() is null i.e., before onCreateView() or after onDestroyView()
        at androidx.fragment.app.Fragment.getViewLifecycleOwner(Fragment.java:361)
        at com.example.weatherapp.ui.home.HomeFragment.invokeLocationAction(HomeFragment.kt:57)
        at com.example.weatherapp.ui.home.HomeFragment.onStart(HomeFragment.kt:50)
        at androidx.fragment.app.Fragment.performStart(Fragment.java:3021)
        at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:589)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:300)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
        at androidx.fragment.app.FragmentManager.dispatchStart(FragmentManager.java:3079)
        at androidx.fragment.app.FragmentController.dispatchStart(FragmentController.java:262)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:510)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1335)
        at android.app.Activity.performStart(Activity.java:7245)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2912)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1745) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:200) 
        at android.app.ActivityThread.main(ActivityThread.java:6956) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:519) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:836) 
2021-05-25 17:45:26.014 32061-32061/com.example.weatherapp I/Process: Sending signal. PID: 32061 SIG: 9

I assume I've tried to access lifecycleOwner of the fragment view in the wrong place. Please help me with this issue.


Solution

  • Your onCreateView() is not returning the View you just inflated. This means that your Fragment's View is null and viewLifecycleOwner is indeed not valid.

    You should change your onCreateView to return your newly inflated View:

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            homeView = inflater.inflate(R.layout.home_fragment, container, false)
            return homeView
        }