I'm seeing a crash that I am unable to reproduce myself. This crash happens in the splash screen of the app. requireActivity() is returning null.
I am using Navigation component. MainActivity is the first activity started and it's startDestination is the splash fragment.
Main activity is pretty much empty since navigation component handles the first fragment transaction.
class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) }}
Here is my SplashScreenFragment:
class SplashFragment : Fragment() { private val isFirstLaunch get() = PreferenceHelper.getObject<Boolean>(PREF_FIRST_LAUNCH) ?: true private lateinit var binding: FragmentSplashBinding override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { binding = FragmentSplashBinding.inflate(inflater, container, false) return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) // Show loading animation } override fun onResume() { super.onResume() Log.d("TAG", "isFirstLaunch: $isFirstLaunch") Handler().postDelayed({ Log.d("TAG", "postDelayed finished") if (isFirstLaunch){ navigateToA() } else { navigateToB() } }, DELAY_COUNT) } private fun navigateToA(){ startActivity(Intent(requireActivity(), ActivityA::class.java)) requireActivity().overridePendingTransition(0, 0) requireActivity().finish() } private fun navigateToB(){ startActivity(Intent(requireActivity(), ActivityB::class.java)) requireActivity().overridePendingTransition(0, 0) requireActivity().finish() } companion object { const val PREF_FIRST_LAUNCH = "PREF_FIRST_LAUNCH" const val DELAY_COUNT = 4000L }}
I heavily logged the flow of trying to pin down the problem. This is one instance of the crash.
MainActivity : onCreateSplashFragment : onAttachSplashFragment : onViewCreatedMainActivity : onResumeSplashFragment : onResumeSplashFragment : isFirstLaunch: trueSplashFragment : onPauseMainActivity : onPauseSplashFragment : onDetachMainActivity : onDestroyMainActivity : onCreateSplashFragment : onAttachSplashFragment : onViewCreatedMainActivity : onResumeSplashFragment : onResumeSplashFragment : isFirstLaunch: trueSplashFragment : postDelayed finishedSplashFragment : Navigating to ActivtityACRASH
Am I missing something obvious that would cause this to happen?Thanks in advance.
[EDIT]
Fatal Exception: java.lang.IllegalStateException: Fragment SplashFragment not attached to an activity. at androidx.fragment.app.Fragment.requireActivity(Fragment.java:833) at com.package.SplashFragment.navigateToA(SplashFragment.java:80) at com.package.SplashFragment.access$navigateToB(SplashFragment.java:25) at com.package.SplashFragment$onResume$1.run(SplashFragment.java:54) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)