简体   繁体   English

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

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

I have to fragments discount fragment and edit service fragment displayed on Edit Service activity.我必须分割折扣片段并编辑显示在“编辑服务”活动中的服务片段。 the back arrow displayed on discount fragment when i back to edit service fragment the arrow disappear i want to display it to navigate the previous activity from edit service activity.当我返回编辑服务片段时,折扣片段上显示的后退箭头消失了我想显示它以从编辑服务活动中导航上一个活动。

activity layout .........................................................活动布局 ..................................... …………

<?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 code .............................................................活动代码 ..................................... ………………

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

    }



}

navigation ........................................导航 ........................................

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

As per the setupWithNavController(Toolbar, NavController) documentation :根据setupWithNavController(Toolbar, NavController)文档

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.在所有其他目的地,工具栏将显示向上按钮。

If you want to also show the Up button on your start destination (ie, to go to the previous activity), you'd want to use the version that takes an AppBarConfiguration .如果您还想在开始目的地上显示向上按钮(即转到上一个活动),您需要使用采用AppBarConfiguration的版本。

As per the Update UI components documentation on AppBarConfiguration , AppBarConfiguration allows you to set exactly what destinations you want as top level destinations.根据AppBarConfiguration 上更新 UI 组件文档AppBarConfiguration允许您准确设置您想要的目标作为顶级目标。 To get the Up button to show on every destination, you'd use an empty set of top level destinations:要让向上按钮显示在每个目的地,您需要使用一组空的顶级目的地:

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

Note that since you're using setSUpportActionBar() , you should follow the Action Bar documentation and use the setupActionBarWithNavController() method rather than the Toolbar version.请注意,由于您使用的是setSUpportActionBar() ,因此您应该遵循操作栏文档并使用setupActionBarWithNavController()方法而不是Toolbar版本。 You must also override onSupportNavigateUp() to handle the up button.您还必须覆盖onSupportNavigateUp()来处理向上按钮。

Therefore your complete code would look like:因此,您的完整代码如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我使用底部导航视图时如何从主页片段中的操作栏中删除后退箭头 - How to remove back arrow from action bar in the home fragment when I'm using bottom navigation view 使用导航组件将返回箭头添加到片段 - Add back arrow to fragment with Navigation Component 如何使用 Android Jetpack 的导航组件禁用后退导航并删除 Fragment 上的后退箭头? - How do I disable back navigation and remove the back arrow on a Fragment, using Android Jetpack's Navigation component? 避免通过使用导航组件按后退箭头来重新创建 Fragment - Avoid recreating of Fragment by press back arrow using Navigation component Android:当用户在导航组件、单活动应用程序中点击片段 B 时更新片段 A - Android: Update Fragment A when user hits back on Fragment B in Navigation Component, Single activity app 如何正确处理拦截片段/活动向上导航顶部后退箭头(&lt;-),以便片段/活动仅根据条件关闭 - How to properly handle intercepting a Fragment's/ Activity up navigation top back arrow( <- ) so fragment/Activity only closes based on a condition Android 导航组件动画 go 从活动返回片段 - Android navigation component animate go back from activity to fragment 导航抽屉:返回活动时当前导航片段未更新 - Navigation Drawer : current navigation fragment not updated when back to the activity 从片段返回时,不会调用主页活动的 OnResume() - OnResume() is not called of the home activity when back pressed from a fragment 使用导航组件导航到自己时如何显示后退按钮? - How to display back button when navigating to self with navigation component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM