Android体系结构组件导航:工具栏后退按钮丢失,后退不起作用 [英] Android architecture component navigation: toolbar back button missing, back not working

查看:95
本文介绍了Android体系结构组件导航:工具栏后退按钮丢失,后退不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试jetpack导航,但是移至新片段时无法显示导航后退按钮.

I'm trying the jetpack navigation and can't show the navigation back button when I move to a new fragment.

NavigationActivity.kt

NavigationActivity.kt

class NavActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_navigation)

        val toolbar = findViewById<Toolbar>(R.id.toolbar)
        setSupportActionBar(toolbar)

        val host: NavHostFragment = supportFragmentManager
                .findFragmentById(R.id.navigation_graph) as NavHostFragment? ?: return

        // Set up Navigation
        val navController = host.navController
        setupActionBarWithNavController(navController)
        setupBottomNavMenu(navController)

    }

    private fun setupActionBarWithNavController(navController: NavController) {
        setupActionBarWithNavController(this, navController)
    }

    private fun setupBottomNavMenu(navController: NavController) {
        findViewById<BottomNavigationView>(R.id.bottom_nav_view)?.let { bottomNavView ->
            NavigationUI.setupWithNavController(bottomNavView, navController)
        }
    }

}

activity_navigation.xml

activity_navigation.xml

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout
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=".views.NavActivity">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="?attr/actionBarSize"
    android:elevation="4dp"
    android:background="?attr/colorPrimary"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:layout_width="match_parent">
</androidx.appcompat.widget.Toolbar>
<fragment
    android:id="@+id/navigation_graph"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar"
    app:navGraph="@navigation/navigation_graph"/>

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:menu="@menu/menu_bottom_nav" />

navigation_graph.xml

navigation_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"
    app:startDestination="@+id/launcher_home">
    <fragment
        android:id="@+id/launcher_home"
        android:name="com.noisyninja.androidlistpoc.views.main.MainFragment"
        android:label="@string/app_name"
        tools:layout="@layout/fragment_main">

        <action
            android:id="@+id/next_action"
            app:destination="@+id/detailFragment"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right" />

    </fragment>

    <fragment
        android:id="@+id/detailFragment"
        android:name="com.noisyninja.androidlistpoc.views.detail.DetailFragment"
        android:label="DetailFragment" />
</navigation>

导航代码:

    /**
     * opens detail activity
     */
    override fun showDetail(view: View, me: Me) {

        var bundle = Bundle()
        bundle.putString("key", "value")
        Navigation.findNavController(view).navigate(R.id.next_action, bundle, null)
    }

如上所述,当导航到第二个片段时,工具栏完全丢失,并且不显示后退按钮. 点击硬件后退按钮也不会弹出详细视图. 第一个匹配项无效,第二个匹配项退出了应用程序.

As above when navigation to the second fragment the toolbar goes missing entirely and doesn't show the back button. Hitting the hardware back button also doesn't pop the detail view. The first hit has no effect, the second hit quits the app.

推荐答案

编辑:您的DetailFragment包含以下行

DataBindingUtil.setContentView<FragmentDetailBinding>(requireActivity(),
    R.layout.fragment_detail)

这会将活动"的内容重置为仅作为fragment_detail布局,从而清除了NavHostFragment和其他所有内容.

which is resetting the content of your Activity to only be your fragment_detail layout, wiping out the NavHostFragment and everything else.

您应该改用DataBindingUtil.bind<FragmentDetailBinding>(view)!!.

原始答案(您仍然应该这样做,但以上答案实际上是解决问题的方法)

Original answer (you should still do this, but the above answer is actually what solves the problem)

您的ConstraintLayout缺少很多约束(您的视图应为

Your ConstraintLayout is missing quite a few constraints (your views should be a vertical chain, where every app:layout_constraintTop_toBottomOf has an alternative app:layout_constraintBottom_toTopOf on the other element, etc.).

由于只有一组三个垂直对齐的项目,因此不需要ConstraintLayout-仅LinearLayout就足够了:

Since you have a single set of three vertically aligned items, you don't need a ConstraintLayout - just a LinearLayout is enough:

<?xml version="1.0" encoding="utf-8"?> 
<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"
    android:orientation="vertical"
    tools:context=".views.NavActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="?attr/actionBarSize"
        android:elevation="4dp"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:layout_width="match_parent"/>

    <fragment
        android:id="@+id/navigation_graph"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:navGraph="@navigation/navigation_graph"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/menu_bottom_nav" />
</LinearLayout>

这篇关于Android体系结构组件导航:工具栏后退按钮丢失,后退不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆