android - 为什么我的应用在实现导航图时在 mainActivity 中添加片段后似乎没有响应?
问题描述
我正在尝试学习如何使用导航控制器。问题是,我的应用程序似乎没有响应,显示主要活动需要很长时间,而且我也不能按后退按钮(似乎没有活动)
这是我的导航图的屏幕截图,我正在尝试在我的 Main Activity 中设置主机片段,如您所见,底部导航视图和工具栏在 NavHostFragment 上看起来有点奇怪。
这是我的 mainActivity xml:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme"
android:minHeight="?attr/actionBarSize"
android:id="@+id/toolbar"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<fragment
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/nav_host_fragment"
app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/navigation_graph"
app:defaultNavHost="true"
/>
<android.support.design.widget.BottomNavigationView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@color/colorPrimary"
app:itemIconTint="@color/color_bottom_view_navigation"
app:itemTextColor="@color/color_bottom_view_navigation"
app:menu="@menu/menu_bottom_view"
app:labelVisibilityMode="labeled"
android:id="@+id/bottom_nav"/>
</android.support.constraint.ConstraintLayout>
这是导航图的xml:
<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/navigation_graph"
app:startDestination="@id/mainActivity">
<activity android:id="@+id/mainActivity" android:name="com.muchammadagunglaksana.navcontroller.MainActivity"
android:label="activity_main" tools:layout="@layout/activity_main"/>
</navigation>
这是我的 MainActivity 类:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navController = Navigation.findNavController(this, R.id.nav_host_fragment)
setupBottomNavMenu(navController)
setupActionBar(navController)
}
private fun setupBottomNavMenu(navController: NavController) {
bottom_nav?.let {
NavigationUI.setupWithNavController(it, navController)
}
}
private fun setupActionBar(navController: NavController) {
NavigationUI.setupActionBarWithNavController(this, navController)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_toolbar, menu)
return true
}
}
这里出了什么问题?
解决方案
这是因为您在导航主机片段中添加了与起始目的地相同的活动(mainActivity),这也是主要活动的一部分。
解决方案是您必须删除主要活动作为您的起始目的地并在其中添加 anotehr 片段/活动。像这样:
<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/navigation_graph"
app:startDestination="@id/mainFragment">
<fragment android:id="@+id/mainFragment"
android:name="com.cinderellaman.general.ui.fragments.MainFragment"
android:label="main_fragment"
tools:layout="@layout/main_fragment"/></navigation>