(导航组件)如何在回到主页片段时在活动中显示返回箭头?

6
我需要在“编辑服务活动”上显示两个碎片,一个是折扣碎片,另一个是编辑服务碎片。 当我从编辑服务碎片返回到折扣碎片时,返回箭头消失了,我想要重新显示它以便从“编辑服务活动”中导航回先前的活动。
活动布局 .........................................................
<?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>

活动代码 .............................................................

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>
1个回答

12
根据setupWithNavController(Toolbar, NavController)文档
引导图的起始目标被认为是唯一的顶级目标。在所有其他目标上,工具栏将显示向上按钮。
如果您还想在起始目标上显示向上按钮(即返回到上一个活动),则需要使用带有AppBarConfiguration参数的版本。
根据AppBarConfiguration更新UI组件文档AppBarConfiguration允许您设置您想要作为顶级目标的确切目标。要在每个目标上显示向上按钮,可以使用空集合作为顶级目标。
AppBarConfiguration appBarConfiguration =
    new AppBarConfiguration.Builder().build();

请注意,由于您正在使用setSupportActionBar(),因此应遵循Action Bar文档并使用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();
    }
}

运行得非常好,这是一个完美的解决方案! - Stanley Ko
谢谢。让我困惑的是文档中的第一部分。我不知道起始目的地被视为顶级,因此没有导航按钮。 - Daniel
如果你想知道如何在主目的地时捕获后退按键,请检查是否已弹出backstack,如果没有弹出,则说明你在主目的地,如此处所述:https://dev59.com/JFUK5IYBdhLWcg3w9zyJ#63059587fun onSupportNavigateUp:Boolean{ if(navCont.navigateUp()==false) finish()} - Amr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接