(导航组件)返回首页片段时如何在活动上显示后退箭头? [英] (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>
推荐答案
按照 导航图的起始目标被认为是唯一的顶级目标.在所有其他目标上,工具栏将显示向上"按钮.
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上更新UI组件文档,<通过code> AppBarConfiguration ,您可以准确地将所需的目的地设置为顶级目的地.要使向上"按钮显示在每个目的地上,请使用一组空的顶级目的地: As per the Update UI components documentation on AppBarConfiguration, 请注意,由于您使用的是 Note that since you're using 因此,您的完整代码如下: Therefore your complete code would look like: 这篇关于(导航组件)返回首页片段时如何在活动上显示后退箭头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
AppBarConfiguration
的版本.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();
setSUpportActionBar()
,因此应遵循操作栏文档,并使用 setupActionBarWithNavController()
方法而不是 Toolbar
版本.您还必须覆盖 onSupportNavigateUp()
来处理向上按钮.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.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();
}
}