Reputation: 26821
My layout file presents a list of stores (see screenshot).
I have set a click listener in my layout file with Data Binding like this:
android:onClick="@{() -> storeCallback.onStoreClick(store)}"
In my Fragment, I have defined this class like this:
class StoreClick(val block: (StoreModel) -> Unit) {
/**
* Called when a store is clicked
*
* @param store the store that was clicked
*/
fun onStoreClick(store: StoreModel) = block(store)
}
And I am invoking it in the adapter like this:
StoreClick {
// When a store is clicked this block or lambda will be called by MyAdapter
val action =
MyFragmentDirections.actionHomeToStoreDetailFragment(it.storeID)
findNavController().navigate(action)
})
Everything works fine before configuration change. However, after rotating the device and clicking on a store item in the adapter, the app crashes because of findNavController
with this error:
no current navigation code
Any ideas?
UPDATE: I detected the error in AOSP here. However, it is not clear what is happening in this case.
Upvotes: 1
Views: 278
Reputation: 26821
For anyone who is interested, I solved this problem. Lint in Android Studio Canary was telling me to use a FragmentContainerView instead of a Fragment
in the NavHost activity xml layout. That's what caused the issue. Once I changed the NavGraph's enclosing tag back to Fragment
, it works:
Upvotes: 2