1

I have one MainActivity with two fragments, FirstFragment and SecondFragment, in my Activity i have an onOptionItemSelected handler from which the user should be able to navigate to settings.

The issue is that the handler looks like this:

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.
    return when (item.itemId) {
        R.id.action_settings -> {
            val navHostFragment =
                    supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
            val navController = navHostFragment.navController
            navController.navigate(R.id.action_FirstFragment_to_settingsActivity)
            return true
        }
        else -> super.onOptionsItemSelected(item)
    }
}

So i have no problems to navigate from FirstFragment to SettingsActivity, but when i try to navigate to Settings activity from my SecondFragment my app crash as the navigate action is set only for first fragment...

So in that handler how can i check in which fragment i'm and cast the action_SecondFragment_to_settingsActivity?

3

2 Answers 2

2

Based on my understanding, NavigationComponent is based on single-activity architecture.

If you need to navigate to another activity, you need to use startActivity(Intent(this, SecondActivity::class.java)) instead of calling navController.

Besides, you can also create another nav_graph for SecondActivity

Sign up to request clarification or add additional context in comments.

2 Comments

SecondActivity is actually yet in nav_graph
I found a good explanation on this, kindly checkout stackoverflow.com/questions/53717028/…
-1

The solution was more simpler than i tought, i just changed the id of the action in nav_graph and i set the same id for both fragments, so from:

    <action
        android:id="@+id/FirstFragment_to_settingsActivity"
        app:destination="@id/settingsActivity"
        app:enterAnim="@anim/fragment_fade_enter"
        app:exitAnim="@anim/fragment_fade_exit"
        app:popEnterAnim="@anim/fragment_fade_enter"
        app:popExitAnim="@anim/fragment_fade_exit" />

It become

    <action
        android:id="@+id/open_settingsActivity"
        app:destination="@id/settingsActivity"
        app:enterAnim="@anim/fragment_fade_enter"
        app:exitAnim="@anim/fragment_fade_exit"
        app:popEnterAnim="@anim/fragment_fade_enter"
        app:popExitAnim="@anim/fragment_fade_exit" />

And same for SecondFragment i set just the id from secondFragment_to_settingActivity to open_settingsActivity

1 Comment

can you confirm that the navController is the same in FirstActivity?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.