我一直在开发一个应用程序,并且遇到了这个问题,我试图在演示应用程序中重新创建。请看一下这个演示应用程序的简单代码。
列表片段.kt
class ListFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = DataBindingUtil.inflate<FragmentListBinding>(
inflater, R.layout.fragment_list, container, false)
binding.buttonGoToDetail.setOnClickListener {
findNavController().navigate(ListFragmentDirections.actionNavigateToDetailFragment())
}
return binding.root
}
细节片段.kt
class DetailFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = DataBindingUtil.inflate<FragmentDetailBinding>(
inflater, R.layout.fragment_detail, container, false)
return binding.root
}
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
appBarConfiguration = AppBarConfiguration(navController.graph)
setupActionBarWithNavController(navController, appBarConfiguration)
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
}
片段列表.xml
<layout
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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ListFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/description_list_fragment"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="30dp"/>
<Button
android:id="@+id/buttonGoToDetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/detail_page_button_label"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginBottom="30dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
片段详细信息.xml
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DetailiFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/description_detail_fragment" />
</FrameLayout>
</layout>
activity_main.xml
<LinearLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph">
</fragment>
</LinearLayout>
nav_graph.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/nav_graph"
app:startDestination="@id/ListFragment">
<fragment
android:id="@+id/ListFragment"
android:name="com.example.navigationtest1.ListFragment"
android:label="list_fragment"
tools:layout="@layout/fragment_list">
<action
android:id="@+id/action_navigateToDetailFragment"
app:destination="@+id/DetailFragment" />
</fragment>
<fragment
android:id="@+id/DetailFragment"
android:name="com.example.navigationtest1.DetailFragment"
android:label="detail_fragment"
tools:layout="@layout/fragment_detail">
</fragment>
</navigation>
问题
现在,当我运行应用程序时,它甚至会在 startDestination 片段(即 ListFragment)上显示向上按钮。我不知道为什么。据我从文档和 codelab 教程中了解到的,它不应该在 startDestination 片段上显示 Up 按钮。但是,在我的例子中,Up 按钮出现在 ListFragment 和 DetailFragment 上。不过,我必须说,Up 按钮在 ListFragment 页面上没有按应有的方式执行任何操作。
任何帮助解决这个问题都非常感谢。