If I use something like this navController.navigate(R.id.action_nav_home_to_profileFragment)
it does navigate me to destination fragment (from my main fragment) because it has action in mainFragment navigation file, but if I'm in other fragments it does not navigate because it doesn't have actions in those fragments.
My question is: is that possible to navigate to my destination fragment from any fragment without using action id? instead using fragment name?
PS: this button is placed in my topbar that's why I need it to be able to navigate from any fragment to destination fragment.
Code
navigation
<fragment
android:id="@+id/nav_home"
android:name="irando.co.id.kerjaremote.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" >
<argument android:name="projectSlugArgument" />
<action
android:id="@+id/action_nav_home_to_projectDetailsFragment"
app:destination="@id/projectDetailsFragment" />
<argument android:name="searchArgument" />
<action
android:id="@+id/action_nav_home_to_searchFragment"
app:destination="@id/searchFragment" />
<!-- destination action (works only when I'm in main fragment) -->
<action
android:id="@+id/action_nav_home_to_profileFragment"
app:destination="@id/profileFragment" />
</fragment>
<!-- destination fragment -->
<fragment
android:id="@+id/profileFragment"
android:name="irando.co.id.kerjaremote.ui.auth.ProfileFragment"
android:label="fragment_profile"
tools:layout="@layout/fragment_profile" />
mainActivity
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle presses on the action bar menu items
when (item.itemId) {
R.id.action_profile -> {
navController.navigate(R.id.action_nav_home_to_profileFragment)
}
}
return super.onOptionsItemSelected(item)
}
Update
How my navigation works:
I am doing this navigation from my MainActivity which loads fragments.
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var sesssion: SessionManager
lateinit var navController: NavController // <--------------------- here
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val fab: FloatingActionButton = findViewById(R.id.fab)
fab.setOnClickListener { view ->
val intent = Intent(this, PublishActivity::class.java)
startActivity(intent)
}
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHostFragment.navController // <--------------------- here
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_home, R.id.nav_my_projects, R.id.nav_my_bids, R.id.nav_slideshow
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
val action_auth = menu.findItem(R.id.action_auth)
val action_profile = menu.findItem(R.id.action_profile)
sesssion = SessionManager(this)
if (sesssion.isLoggedIn()) {
action_auth.isVisible = false
action_profile.isVisible = true
} else {
action_auth.isVisible = true
action_profile.isVisible = false
}
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle presses on the action bar menu items
when (item.itemId) {
R.id.action_auth -> {
val i = Intent(this, AuthActivity::class.java)
startActivity(i)
return true
}
R.id.action_profile -> {
navController.navigate(R.id.action_nav_to_profileFragment) // <--------------------- here (global action)
}
}
return super.onOptionsItemSelected(item)
}
override fun onSupportNavigateUp(): Boolean {
navController = findNavController(R.id.nav_host_fragment) // <--------------------- here
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
// ability of setting top bar title in fragments
fun setActionBarTitle(title: String?) {
supportActionBar!!.title = title
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>