Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(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>
like image 443
user8282703 Avatar asked Feb 11 '20 16:02

user8282703


Video Answer


1 Answers

As per the 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.

If you want to also show the Up button on your start destination (i.e., to go to the previous activity), you'd want to use the version that takes an AppBarConfiguration.

As per the Update UI components documentation on AppBarConfiguration, AppBarConfiguration allows you to set exactly what destinations you want as top level destinations. 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. You must also override onSupportNavigateUp() to handle the up button.

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();
    }
}
like image 105
ianhanniballake Avatar answered Sep 21 '22 12:09

ianhanniballake