Back/Up 使用导航抽屉时箭头不起作用
Back/Up Arrow not working when using navigation drawer
使用导航抽屉时,后退或向上按钮不起作用。图标从汉堡包变化(设置了顶级目的地),但都打开了导航抽屉。我希望 back/up 箭头回到堆栈中。
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, DrawerLocker {
public static final String TAG = MainActivity.class.getSimpleName();
private DrawerLayout drawerLayout;
private NavigationView navView;
private Toolbar toolbar;
private NavController navController;
private ActionBarDrawerToggle drawerToggle;
private AppBarConfiguration appBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//configure drawer layout
drawerLayout = findViewById(R.id.drawer_layout);
navView = findViewById(R.id.drawer_navigation_view);
navController = Navigation.findNavController(this,R.id.nav_host_fragment);
appBarConfiguration = new AppBarConfiguration.Builder(R.id.scenarioListFragment,
R.id.tokenListFragment, R.id.settingsFragment,R.id.historyFragment)
.setDrawerLayout(drawerLayout)
.build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
navView.setNavigationItemSelectedListener(this);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(drawerToggle);
drawerToggle.syncState();
if (savedInstanceState == null ) {
navView.setCheckedItem(R.id.drawer_menu_scenario);
toolbar.setTitle("Scenarios");
}
}
我看了this video,这看起来有点简单,但它指的是 Kotlin,我的代码也不远。
也涵盖了类似的挑战,尽管在 Kotlin 中我尝试添加一个 setNavigationOnClickListener 并在其中放置一个 logd 语句。它从未被解雇,但解决方案本身似乎没有必要。
This post 似乎涵盖了相同的问题,但没有答案。其中一个回复是对 setupActionBarWithNavController 说的,我有:
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
这个函数有很多重载,我已经尝试了很多重载,但它们似乎导致我丢失了一组顶级目的地(多个片段的汉堡包图标)并且我的导航不当正在工作。
我试图跟随 this documentation,但它似乎只是让我从原来的位置退后一步。此调用:
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder(navController.getGraph())
.setDrawerLayout(drawerLayout)
.build();
似乎要替换 topLevelDestinations 并使用 navController.getGraph(),但由于我有多个,所以我一直在引用 fragments themselves.
我之前曾 ask 解决过这个问题,但我很清楚我没有很好地解释它(答案解决了如何弹出而不是为什么后退箭头不起作用)。我会删除它,但人们确实回答了,SO 提供的指导是在这种情况下我不应该删除它。如果造成混淆,我很抱歉。
有两个问题:
When using NavigationUI
, the top app bar helpers automatically transition between the drawer icon and the Up icon as the current destination changes. You don't need to use ActionBarDrawerToggle
.
因此您必须删除所有 ActionBarDrawerToggle
代码。
override onSupportNavigateUp()
to handle Up navigation:
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this,
R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
请注意,如果您已经 included an android:label
on your destinations 并且正在使用 setupWithNavController
,那么您的 if (savedInstanceState == null)
也是不必要的。
使用导航抽屉时,后退或向上按钮不起作用。图标从汉堡包变化(设置了顶级目的地),但都打开了导航抽屉。我希望 back/up 箭头回到堆栈中。
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, DrawerLocker {
public static final String TAG = MainActivity.class.getSimpleName();
private DrawerLayout drawerLayout;
private NavigationView navView;
private Toolbar toolbar;
private NavController navController;
private ActionBarDrawerToggle drawerToggle;
private AppBarConfiguration appBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//configure drawer layout
drawerLayout = findViewById(R.id.drawer_layout);
navView = findViewById(R.id.drawer_navigation_view);
navController = Navigation.findNavController(this,R.id.nav_host_fragment);
appBarConfiguration = new AppBarConfiguration.Builder(R.id.scenarioListFragment,
R.id.tokenListFragment, R.id.settingsFragment,R.id.historyFragment)
.setDrawerLayout(drawerLayout)
.build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
navView.setNavigationItemSelectedListener(this);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(drawerToggle);
drawerToggle.syncState();
if (savedInstanceState == null ) {
navView.setCheckedItem(R.id.drawer_menu_scenario);
toolbar.setTitle("Scenarios");
}
}
我看了this video,这看起来有点简单,但它指的是 Kotlin,我的代码也不远。
This post 似乎涵盖了相同的问题,但没有答案。其中一个回复是对 setupActionBarWithNavController 说的,我有:
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
这个函数有很多重载,我已经尝试了很多重载,但它们似乎导致我丢失了一组顶级目的地(多个片段的汉堡包图标)并且我的导航不当正在工作。
我试图跟随 this documentation,但它似乎只是让我从原来的位置退后一步。此调用:
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder(navController.getGraph())
.setDrawerLayout(drawerLayout)
.build();
似乎要替换 topLevelDestinations 并使用 navController.getGraph(),但由于我有多个,所以我一直在引用 fragments themselves.
我之前曾 ask 解决过这个问题,但我很清楚我没有很好地解释它(答案解决了如何弹出而不是为什么后退箭头不起作用)。我会删除它,但人们确实回答了,SO 提供的指导是在这种情况下我不应该删除它。如果造成混淆,我很抱歉。
有两个问题:
When using
NavigationUI
, the top app bar helpers automatically transition between the drawer icon and the Up icon as the current destination changes. You don't need to useActionBarDrawerToggle
.
因此您必须删除所有 ActionBarDrawerToggle
代码。
override
onSupportNavigateUp()
to handle Up navigation:
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this,
R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
请注意,如果您已经 included an android:label
on your destinations 并且正在使用 setupWithNavController
,那么您的 if (savedInstanceState == null)
也是不必要的。