[英](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.