'IllegalStateException on DataBindingUtil.setContentView: Restoring the Navigation back stack failed, current destination null
For some users, my app seems to crash regularly at or near app start. It's been an issue I've been trying to figure out for a while but I haven't had much success since I haven't been able to reproduce it on any of our test devices and this is also my first time working with the AndroidX Navigation components.
It happens on this line:
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
And this is the relevant stack trace from crashlytics, the issue looks like this:
Caused by java.lang.IllegalStateException
Restoring the Navigation back stack failed: destination com.company.app:id/home_navigation cannot be found from the current destination null
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.app/com.company.app.MainActivity}: android.view.InflateException: Binary XML file line #54 in com.company.app:layout/activity_main: Binary XML file line #54 in com.company.app/activity_main: Error inflating class fragment
In my activity_main.xml, this is the fragment that it is failing to inflate and line 54 is the last line:
<fragment
android:id="@+id/nav_host_fragment"
class="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/home_navigation" />
So @navigation/home_navigation, the navigation graph, cannot be found in the current destination, which means what exactly? How do I stop it from attempting to navigate? How do I make sure it doesn't crash? Why is this happening?
Solution 1:[1]
This most likely means, that it fails to parse XML of navigation/home_navigation.xml
.
You may replace the fragment
with a androidx.fragment.app.FragmentContainerView
.
And it should also generate a named binding ...
ActivityMainBinding.inflate(inflater, container, false);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Martin Zeitler |