【发布时间】:2021-04-22 02:21:42
【问题描述】:
如果我使用类似 navController.navigate(R.id.action_nav_home_to_profileFragment) 的东西,它会导航到目标片段(从我的主片段),因为它在 mainFragment 导航文件中有操作,但如果我在其他片段中,它不会导航,因为它没有这些片段中没有动作。
我的问题是:是否可以在不使用操作 ID 的情况下从任何片段导航到我的目标片段?而是使用片段名称?
PS:这个按钮位于我的顶部栏中,这就是为什么我需要它能够从任何片段导航到目标片段。
代码
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)
}
更新
我的导航是如何工作的:
我正在从加载片段的 MainActivity 进行此导航。
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>
【问题讨论】:
标签: android kotlin android-fragments android-navigation