BottomNavigationView.setupWithNavController 不工作
BottomNavigationView.setupWithNavController not working
目前我正在开发一个 Android 应用程序,我想在其中同时使用 Android Navigation Component
和 BottomNavigationView
。
在开发上述应用程序时,我查看了官方代码实验室并在此处提出了几个问题,但事实证明它们没有任何帮助。
问题是将 navController
分配给我的 BottomNavigationView
似乎没有任何效果 - 单击菜单项不会影响导航主机。
我的代码:
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayoutxmlns: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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/main_host_fr"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_nav_bnv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@android:color/transparent"
android:elevation="0dp"
app:elevation="0dp"
app:menu="@menu/menu_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MainActivity.kt
import android.os.Bundle
import android.os.PersistableBundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.setupWithNavController
import com.aredruss.qurio.databinding.ActivityMainBinding
import com.aredruss.qurio.helpers.viewBinding
class MainActivity : AppCompatActivity() {
// ViewBindingDelegate
private val binding: ActivityMainBinding by viewBinding(ActivityMainBinding::inflate)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
// Using onPostCreate because accessing NavHost in OnCreate causes crashes
override fun onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onPostCreate(savedInstanceState, persistentState)
binding.navigationBb.setupWithNavController(findNavController(R.id.main_host_fr))
NavigationUI.setupWithNavController(binding.mainNavBnv, findNavController(R.id.main_host_fr))
}
}
nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
app:startDestination="@id/action_history">
<fragment
android:id="@+id/action_history"
android:name="com.aredruss.qurio.ui.HistoryFragment"
android:label="HistoryFragment"
tools:layout="@layout/fragment_history" >
</fragment>
<fragment
android:id="@+id/action_settings"
android:name="com.aredruss.qurio.ui.SettingsFragment"
android:label="SettingsFragment"
tools:layout="@layout/fragment_settings" >
</fragment>
</navigation>
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_history"
android:icon="@drawable/ic_history"
android:title="History"
app:showAsAction="withText" />
<item
android:id="@+id/action_settings"
android:title="Settings"
android:icon="@drawable/ic_settings"
app:showAsAction="withText"
/>
</menu>
我已经检查了 this 解决方案,但没有任何帮助。
以下是需要解决的问题:
- 您正在使用自动处理
setContentView
的 ViewBindingDelegate
,因此您需要删除它,因为在这种情况下您将无法使用 binding.xx
:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_main) //<<<<< remove this
}
Called when activity start-up is complete (after onStart() and onRestoreInstanceState(Bundle) have been called). Applications will generally not implement this method; it is intended for system classes to do final initialization after application code has run.
根据我的测试,onPostCreate()
没有被调用。因此,要解决此问题,请将这部分代码移动到可以由 onStart()
或 onResume()
:
等开发人员实现的生命周期方法
override fun onStart() {
super.onStart()
NavigationUI.setupWithNavController(
binding.mainNavBnv,
findNavController(R.id.main_host_fr)
)
}
这样 NavController
可以自动处理 BottomNavigationView
:
UPDATE:根据评论,您应该按如下方式使用 NavHostFragment 获取 NavController
,因此您可以将代码传输到 onCreate()
方法:
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.main_host_fr) as NavHostFragment
val navController = navHostFragment.navController
你覆盖错了onPostCreate()
:
override fun onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
您不想要带 PersistableBundle
的那个 - 您想要普通的:
override fun onPostCreate(savedInstanceState: Bundle?) {
现在您的 onPostCreate()
将真正被调用。
当然,您根本不应该使用 onPostCreate()
。相反,您应该是 following the documentation,它明确说明您应该使用以下代码在 onCreate()
中获取 NavController
:
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.main_host_fr) as NavHostFragment
val navController = navHostFragment.navController
这让您可以将整个代码编写为:
// ViewBindingDelegate
private val binding: ActivityMainBinding by viewBinding(ActivityMainBinding::inflate)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Note how we actually set the content view from the binding
setContentView(binding.root)
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.main_host_fr) as NavHostFragment
val navController = navHostFragment.navController
binding.navigationBb.setupWithNavController(navController)
binding.mainNavBnv.setupWithNavController(navController)
}
错误导入工具栏时会出现该问题所以首先,尝试删除工具栏导入并将该行添加到栏导入
androidx.appcompat.widget.Toolbar Android X 工具栏
目前我正在开发一个 Android 应用程序,我想在其中同时使用 Android Navigation Component
和 BottomNavigationView
。
在开发上述应用程序时,我查看了官方代码实验室并在此处提出了几个问题,但事实证明它们没有任何帮助。
问题是将 navController
分配给我的 BottomNavigationView
似乎没有任何效果 - 单击菜单项不会影响导航主机。
我的代码:
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayoutxmlns: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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/main_host_fr"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_nav_bnv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@android:color/transparent"
android:elevation="0dp"
app:elevation="0dp"
app:menu="@menu/menu_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MainActivity.kt
import android.os.Bundle
import android.os.PersistableBundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.setupWithNavController
import com.aredruss.qurio.databinding.ActivityMainBinding
import com.aredruss.qurio.helpers.viewBinding
class MainActivity : AppCompatActivity() {
// ViewBindingDelegate
private val binding: ActivityMainBinding by viewBinding(ActivityMainBinding::inflate)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
// Using onPostCreate because accessing NavHost in OnCreate causes crashes
override fun onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onPostCreate(savedInstanceState, persistentState)
binding.navigationBb.setupWithNavController(findNavController(R.id.main_host_fr))
NavigationUI.setupWithNavController(binding.mainNavBnv, findNavController(R.id.main_host_fr))
}
}
nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
app:startDestination="@id/action_history">
<fragment
android:id="@+id/action_history"
android:name="com.aredruss.qurio.ui.HistoryFragment"
android:label="HistoryFragment"
tools:layout="@layout/fragment_history" >
</fragment>
<fragment
android:id="@+id/action_settings"
android:name="com.aredruss.qurio.ui.SettingsFragment"
android:label="SettingsFragment"
tools:layout="@layout/fragment_settings" >
</fragment>
</navigation>
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_history"
android:icon="@drawable/ic_history"
android:title="History"
app:showAsAction="withText" />
<item
android:id="@+id/action_settings"
android:title="Settings"
android:icon="@drawable/ic_settings"
app:showAsAction="withText"
/>
</menu>
我已经检查了 this 解决方案,但没有任何帮助。
以下是需要解决的问题:
- 您正在使用自动处理
setContentView
的ViewBindingDelegate
,因此您需要删除它,因为在这种情况下您将无法使用binding.xx
:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_main) //<<<<< remove this
}
Called when activity start-up is complete (after onStart() and onRestoreInstanceState(Bundle) have been called). Applications will generally not implement this method; it is intended for system classes to do final initialization after application code has run.
根据我的测试,onPostCreate()
没有被调用。因此,要解决此问题,请将这部分代码移动到可以由 onStart()
或 onResume()
:
override fun onStart() {
super.onStart()
NavigationUI.setupWithNavController(
binding.mainNavBnv,
findNavController(R.id.main_host_fr)
)
}
这样 NavController
可以自动处理 BottomNavigationView
:
UPDATE:根据评论,您应该按如下方式使用 NavHostFragment 获取 NavController
,因此您可以将代码传输到 onCreate()
方法:
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.main_host_fr) as NavHostFragment
val navController = navHostFragment.navController
你覆盖错了onPostCreate()
:
override fun onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
您不想要带 PersistableBundle
的那个 - 您想要普通的:
override fun onPostCreate(savedInstanceState: Bundle?) {
现在您的 onPostCreate()
将真正被调用。
当然,您根本不应该使用 onPostCreate()
。相反,您应该是 following the documentation,它明确说明您应该使用以下代码在 onCreate()
中获取 NavController
:
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.main_host_fr) as NavHostFragment
val navController = navHostFragment.navController
这让您可以将整个代码编写为:
// ViewBindingDelegate
private val binding: ActivityMainBinding by viewBinding(ActivityMainBinding::inflate)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Note how we actually set the content view from the binding
setContentView(binding.root)
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.main_host_fr) as NavHostFragment
val navController = navHostFragment.navController
binding.navigationBb.setupWithNavController(navController)
binding.mainNavBnv.setupWithNavController(navController)
}
错误导入工具栏时会出现该问题所以首先,尝试删除工具栏导入并将该行添加到栏导入
androidx.appcompat.widget.Toolbar Android X 工具栏