簡體   English   中英

Android 架構組件導航:工具欄后退按鈕丟失,后退不起作用

[英]Android architecture component navigation: toolbar back button missing, back not working

我正在嘗試使用噴氣背包導航,但在移動到新片段時無法顯示導航后退按鈕。

導航活動.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)
        }
    }

}

活動導航.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" />

導航圖.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)
    }

沒有標題欄

如上所述,當導航到第二個片段時,工具欄完全丟失並且不顯示后退按鈕。 點擊硬件后退按鈕也不會彈出詳細信息視圖。 第一次點擊無效,第二次點擊退出應用程序。

編輯:您的DetailFragment包含該行

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

這將您的 Activity 內容重置為您的fragment_detail布局,清除 NavHostFragment 和其他所有內容。

您應該使用DataBindingUtil.bind<FragmentDetailBinding>(view)!! 反而。

原始答案(您仍然應該這樣做,但上面的答案實際上是解決問題的方法)

您的ConstraintLayout缺少很多約束(您的視圖應該是一個垂直鏈,其中每個app:layout_constraintTop_toBottomOf在另一個元素上都有一個替代app:layout_constraintBottom_toTopOf等)。

由於您有一組三個垂直對齊的項目,因此您不需要ConstraintLayout - 只需LinearLayout就足夠了:

<?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>

使用nav_host_fragment在您的 Activity 中Override此方法

@Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM