IgorGanapolsky
IgorGanapolsky

Reputation: 26821

Android NavController error `no current navigation code`

My layout file presents a list of stores (see screenshot).
enter image description here

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

Answers (1)

IgorGanapolsky
IgorGanapolsky

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: enter image description here

Upvotes: 2

Related Questions