'Up button appears on the start destination fragment in Navigation
I have been working on an app and I have hit this problem which I tried to recreate in a demo app. Please have a look at the simple code of this demo app.
ListFragment.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
}
DetailFragment.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()
}
}
fragment_list.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>
fragment_detail.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>
Problem
Now, when I run the app, it displays the Up button even on the startDestination fragment (which is the ListFragment). I can't figure out why. As much as I understood from the documentation and the codelab tutorial, it should not display the Up button on the startDestination fragment. But, in my case, the Up button appears on the ListFragment and also on the DetailFragment. Though, I must say, the Up button does nothing on the ListFragment page as it is supposed to be.
Any help figuring this out is really appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
