dave selby
dave selby

Reputation: 145

Navigation.findNavController() Cant find view

Android newbe here, I have am trying to get a menu option in the inflated menu in MainActivity to trigger a navigation to another screen, the issue I am having is finding the view of MainActivity for findNavController() ,I need it where I have put the ????, can anyone help ?

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setSupportActionBar(findViewById(R.id.toolbar))
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the menu; this adds items to the action bar if it is present.
    menuInflater.inflate(R.menu.menu_main, menu)
    return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    when (item.itemId) {

        R.id.menu_settings -> {
            val controller: NavController = Navigation.findNavController(????)
            controller.navigate(R.id.settingsFragment)
        }

        else -> super.onOptionsItemSelected(item)
    }
    return true
}

}

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.ebookfrenzy.sos.MainActivity">
<item
    android:id="@+id/menu_settings"
    android:orderInCategory="11"
    android:title="@string/action_settings"
    app:showAsAction="never" />

<item
    android:id="@+id/menu_removeads"
    android:orderInCategory="20"
    android:title="@string/remove_ads"
    app:showAsAction="never" />

<item
    android:id="@+id/menu_about"
    android:orderInCategory="20"
    android:title="@string/about"
    app:showAsAction="never"/>

Upvotes: 1

Views: 3675

Answers (1)

madlymad
madlymad

Reputation: 6530

As advised in the documentation to do this you require one of the following:

  • Fragment.findNavController()
  • View.findNavController()
  • Activity.findNavController(viewId: Int)

How to use the above information:

Your code is located in MainActivity so the function you have to call is the 3rd Activity.findNavController(viewId: Int) the ???? is the viewId.

What is the viewId:

viewId is R.id.theIdentifierYouSetInYourXML so you have to navigate in your code to the layout you set in method setContentView this layout is called R.layout.activity_main or in xml words activity_main.xml. Within this file there should be something like the following:

<?xml version="1.0" encoding="utf-8"?>
<!-- irrelevant code  -->

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        
...

        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

    <!-- irrelevant code  -->

The FragmentContainerView is important here. We require its id. In my example case above the answer will be R.id.nav_host_fragment.

Putting all together:

Navigation.findNavController(????) will be Activity.findNavController(R.id.nav_host_fragment) but you have to check your code in activity_main.xml to find your own id.

If you don't have this code in your activity_main.xml then I would suggest to start from here.

Upvotes: 1

Related Questions