(Navigation component) 如何在返回主页片段时在 activity 上显示返回箭头?

(Navigation component) How to display back arrow on the activity when back to the home fragment?

我必须对编辑服务 activity 上显示的折扣片段和编辑服务片段进行片段化。 当我返回编辑服务片段时显示在折扣片段上的后退箭头箭头消失我想显示它以导航来自编辑服务 activity.

的前一个 activity

activity布局 ..................................................... ......

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.appcompat.widget.Toolbar

            android:id="@+id/toolbar_hesham"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:title="@string/edit_service"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:layout_constraintTop_toTopOf="parent" />

        <fragment
            android:id="@+id/nav_host_edit_service"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toBottomOf="@+id/toolbar_hesham"
            app:layout_constraintBottom_toBottomOf="parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/edit_service_nav" />
    </androidx.constraintlayout.widget.ConstraintLayout>


    <include layout="@layout/confirm_request_bottom_sheet"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

activity代码 ..................................................... ............

public class EditServicesActivity extends BaseActivity<MainViewModel> {


    public Toolbar toolbar;
    public NavController navController;

    @Override
    protected void initActivityComponent() {
        component = ProServeTechApp.getComponent(this)
                .plus(new ActivityModule(this));
        component.inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_edit_services;
    }

    @Override
    protected Class<MainViewModel> getViewModelClass() {
        return MainViewModel.class;
    }

    @Override
    protected void initActivity() {
        viewModel.getRequestDetails(getIntent().getExtras().getString("requestId"), String.valueOf(0));
        viewModel.getIssues(getIntent().getStringExtra("requestId"));

        toolbar = findViewById(R.id.toolbar_hesham);
        setSupportActionBar(toolbar);

        navController = Navigation.findNavController(this, R.id.nav_host_edit_service);
        NavigationUI.setupWithNavController(toolbar, navController );

    }



}

导航 .....................................

<?xml version="1.0" encoding="utf-8"?>
<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/edit_service_nav"
    app:startDestination="@id/edi_service_fragment">

    <fragment
        android:id="@+id/edi_service_fragment"
        android:name="com.unicomg.proservetech.ui.requestdetails.edit.service.fragments.EditServiceFragment"
        android:label="@string/edit_service"
        tools:layout="@layout/edit_service_fragment">

        <action
            android:id="@+id/action_edi_service_fragment_to_discount_fragment"
            app:destination="@id/discount_fragment"
            app:enterAnim="@anim/slide_up"
            app:exitAnim="@anim/slide_bottom"
            app:popEnterAnim="@anim/slide_up"
            app:popExitAnim="@anim/slide_bottom" />
    </fragment>

    <fragment
        android:id="@+id/discount_fragment"
        android:name="com.unicomg.proservetech.ui.requestdetails.edit.service.fragments.DiscountFragment"
        android:label="@string/add_discount"
        tools:layout="@layout/discount_fragment">


    </fragment>

</navigation>

根据 setupWithNavController(Toolbar, NavController) documentation:

The start destination of your navigation graph is considered the only top level destination. On all other destinations, the Toolbar will show the Up button.

如果您还想在开始目的地显示向上按钮(即转到上一个 activity),您需要使用带有 AppBarConfiguration 的版本。

根据 Update UI components documentation on AppBarConfigurationAppBarConfiguration 允许您准确设置您想要的顶级目的地。要让向上按钮显示在每个目的地,您需要使用一组空的顶级目的地:

AppBarConfiguration appBarConfiguration =
    new AppBarConfiguration.Builder().build();

请注意,由于您使用的是 setSUpportActionBar(),因此您应该遵循 Action Bar documentation 并使用 setupActionBarWithNavController() 方法,而不是 Toolbar 版本。您还必须覆盖 onSupportNavigateUp() 来处理向上按钮。

因此您的完整代码如下所示:

public class EditServicesActivity extends BaseActivity<MainViewModel> {


    public Toolbar toolbar;
    public AppBarConfiguation appBarConfiguation;
    public NavController navController;

    @Override
    protected void initActivityComponent() {
        component = ProServeTechApp.getComponent(this)
                .plus(new ActivityModule(this));
        component.inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_edit_services;
    }

    @Override
    protected Class<MainViewModel> getViewModelClass() {
        return MainViewModel.class;
    }

    @Override
    protected void initActivity() {
        viewModel.getRequestDetails(getIntent().getExtras().getString("requestId"), String.valueOf(0));
        viewModel.getIssues(getIntent().getStringExtra("requestId"));

        toolbar = findViewById(R.id.toolbar_hesham);
        setSupportActionBar(toolbar);

        navController = Navigation.findNavController(this, R.id.nav_host_edit_service);
        appBarConfiguration = new AppBarConfiguration.Builder().build();
        NavigationUI.setupActionBarWithNavController(this, navController,
            appBarConfiguration);
    }

    @Override
    public boolean onSupportNavigateUp() {
        return NavigationUI.navigateUp(navController, appBarConfiguration)
                || super.onSupportNavigateUp();
    }
}