我有一个问题。我正在使用安卓导航架构组件,使用带有 singl 活动的底部导航视图。如何使片段只打开一次?即使导致此片段的按钮被单击了几次? 如何使一个片段只添加到后堆栈一次? 我创建了一个测试项目来尝试一下
XML navigation_test
<?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/test_nav"
app:startDestination="@id/firstFragment">
<fragment
android:id="@+id/firstFragment"
android:name="ru.artem_nr.navigation_test.FirstFragment"
android:label="fragment_first"
tools:layout="@layout/fragment_first" >
<action
android:id="@+id/action_firstFragment_to_secondFrag"
app:destination="@id/navigation"
app:launchSingleTop="true"
app:popUpTo="@+id/secondFrag"
app:popUpToInclusive="false" />
</fragment>
<navigation android:id="@+id/navigation"
app:startDestination="@id/secondFrag">
<fragment
android:id="@+id/secondFrag"
android:name="ru.artem_nr.navigation_test.SecondFrag"
android:label="fragment_second"
tools:layout="@layout/fragment_second" />
</navigation>
</navigation>
main_activity
公共类 MainActivity 扩展了 AppCompatActivity 实现 NavigationView.OnNavigationItemSelectedListener {
public Toolbar toolbar;
public DrawerLayout drawerLayout;
public NavController navController;
public NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupNavigation();
}
private void setupNavigation() {
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
navController = Navigation.findNavController(this, R.id.garden_nav_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout);
NavigationUI.setupWithNavController(navigationView, navController);
navigationView.setNavigationItemSelectedListener((NavigationView.OnNavigationItemSelectedListener) this);
}
@Override
public boolean onSupportNavigateUp() {
return (boolean) NavigationUI.navigateUp(navController, drawerLayout);
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
menuItem.setChecked(true);
drawerLayout.closeDrawers();
int id = menuItem.getItemId();
switch (id) {
case R.id.nav_home:
navController.navigate(R.id.navigation);
break;
}
return true;
}
}
片段 - 只是一个空白
根据此问题,BottomNavigationView
提供了一个优先于NavigationUI
设置的OnNavigationItemSelectedListener
的OnNavigationItemReselectedListener
:
bottomNavigationView.setOnNavigationItemReselectedListener(
new BottomNavigationView.OnNavigationReselectedListener() {
@Override
public void onNavigationItemReselected(MenuItem item) {
// By doing nothing, we ignore reselection events
}
});
NavigationView
不提供与另一个问题相同的接口,因此您需要复制默认行为(请注意,它调用NavigationUI.onNavDestinationSelected()
,而不仅仅是直接调用navigate()
,与您的代码不同)并添加您自己的检查isChecked()
以防止重新选择。
您需要将片段声明为"SingleTop"。
导航图形编辑器视图右侧的菜单上有一个选项可以执行此操作,位于动画选项下方。
或者,您可以通过 NavOptions.Builder 类以编程方式设置它,使用 setLaunchSingleTop(true) 方法,如以下链接中所述:
https://developer.android.com/reference/kotlin/androidx/navigation/NavOptions.Builder#setLaunchSingleTop(科特林。布尔值)
因此,您的选择方法应如下所示:
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem){
NavOptions.Builder builder = new NavOptions.Builder()
.setLaunchSingleTop(true);
NavOptions options = builder.build();
navController.navigate(item.getItemId(), null, options);