簡體   English   中英

(導航組件)返回首頁片段時如何在活動上顯示返回箭頭?

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

我必須分割折扣片段並編輯顯示在“編輯服務”活動中的服務片段。 當我返回編輯服務片段時,折扣片段上顯示的后退箭頭消失了我想顯示它以從編輯服務活動中導航上一個活動。

活動布局 ..................................... …………

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

根據setupWithNavController(Toolbar, NavController)文檔

導航圖的起始目的地被認為是唯一的頂級目的地。 在所有其他目的地,工具欄將顯示向上按鈕。

如果您還想在開始目的地上顯示向上按鈕(即轉到上一個活動),您需要使用采用AppBarConfiguration的版本。

根據AppBarConfiguration 上更新 UI 組件文檔AppBarConfiguration允許您准確設置您想要的目標作為頂級目標。 要讓向上按鈕顯示在每個目的地,您需要使用一組空的頂級目的地:

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

請注意,由於您使用的是setSUpportActionBar() ,因此您應該遵循操作欄文檔並使用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();
    }
}

暫無
暫無

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

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