I'm working on a single-activity application while learning Android dev, and I stumbled on a roadblock, I am trying to launch/navigate to a fragment inside Thread. setDefaultUncaughtExceptionHandler when an exception is thrown, and I can't seem to get it working. I tried the following two options with the Navigation Component and the FragmentManager:
Thread.setDefaultUncaughtExceptionHandler { _, throwable ->
navController.navigate(R.id.exceptionFragment)
}
and
Thread.setDefaultUncaughtExceptionHandler { _, throwable ->
supportFragmentManager.beginTransaction().add(R.id.container, ExceptionFragment()).commit()
}
Both fail to navigate to ExceptionFragment, the app freezes.
I know that it's easily doable if the ExceptionFragment in question was an ExceptionActivity.
Thread.setDefaultUncaughtExceptionHandler { _, throwable ->
val intent = Intent(this@MainActivity, ExceptionActivity::class.java)
startActivity(intent)
}
My intention here is to send the user to another screen informing them that something went wrong with the application, and displaying a button that restarts the app (or MainActivity).
But as said previously, I want to accomplish this behavior with a fragment that's contained inside MainActivity instead. Alternatively if this can't be accomplished with fragments, I might settle with showing a dialog (if that's possible at all), while remaining on a single activity.
The question isn't about whether or not this is considered a bad practice because it obviously is, don't worry, I won't use this in production applications, I am only trying to learn more about activities, fragment, crashes in Android, for the sake of education I am asking this question to the Android gurus on Stackoverflow.