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?

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100
  • 1
    i've updated your title to include the fact that you're using the navigation component – a_local_nobody Jan 19 '21 at 09:10
  • You can check this . i think its helpful https://stackoverflow.com/questions/63007726/navigation-component-how-to-navigate-from-activity-to-a-fragment – Jaber Bin Ahamed Jan 19 '21 at 09:31
  • does this answer your question? https://stackoverflow.com/questions/50689206/how-i-can-retrieve-current-fragment-in-navhostfragment/51962582 – Sekiro Jan 19 '21 at 09:37

2 Answers2

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

Teo
  • 876
  • 9
  • 22
  • SecondActivity is actually yet in nav_graph – NiceToMytyuk Jan 19 '21 at 10:07
  • I found a good explanation on this, kindly checkout https://stackoverflow.com/questions/53717028/how-do-i-link-multiple-activities-in-android-navigation-editor – Teo Jan 19 '21 at 10:10
-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

NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100