【问题标题】:(Navigation component) How to display back arrow on the activity when back to the home fragment?(导航组件)返回首页fragment时如何在activity上显示返回箭头?
【发布时间】:2020-05-27 02:51:06
【问题描述】:

我必须分割折扣片段并编辑编辑服务活动中显示的服务片段。 当我返回编辑服务片段时,折扣片段上显示的后退箭头箭头消失我想显示它以从编辑服务活动导航上一个活动。

活动布局 ..................................................... .......

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

【问题讨论】:

    标签: android android-jetpack


    【解决方案1】:

    根据setupWithNavController(Toolbar, NavController) documentation:

    导航图的起始目的地被认为是唯一的顶级目的地。在所有其他目的地,工具栏将显示向上按钮。

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

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

    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();
        }
    }
    

    【讨论】:

    • 就像一个魅力,它是一个完美的解决方案!
    • 谢谢。吸引我的部分是文档的第一部分。我不知道起始目的地被认为是顶级的,因此没有导航按钮。
    • 如果您想知道当您在主目的地时如何捕捉后压,请检查是否弹出了 backstack,如果没有弹出,那么您就在此处提到的主目的地stackoverflow.com/a/63059587/6039240 fun onSupportNavigateUp:Boolean{ if(navCont.navigateUp()==false) finish()}
    猜你喜欢
    • 1970-01-01
    • 2014-12-26
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 2021-10-27
    • 1970-01-01
    相关资源
    最近更新 更多