将导航UI与导航抽屉一起使用时,片段不会添加到后堆栈

时间:2019-08-10 15:15:53

标签: android android-fragments navigation-drawer android-jetpack android-architecture-navigation

我正在使用Android导航组件创建带有导航抽屉的应用程序。但是,如果通过导航抽屉更改当前片段,然后按回去,则应用程序始终返回到导航图的起始片段。 我只能找到在使用导航组件时如何从Backstack中删除片段的解决方案,而找不到如何添加它们的解决方案。

我已经在NavController的AppBarConfiguration中将其他片段添加为根片段,但这并没有改变行为。 在应用程序的其他部分中使用标准navController.navigate()时,会将片段正确添加到Backstack中。仅当我从导航抽屉中切换片段时,它们才不会添加到Backstack中。

我也尝试过更改片段之间的动作(例如popUpTopopUpToInclusive),但是导航组件似乎并没有使用它们,因为它没有任何改变。

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstaceState);
    setContentView(R.layout.activity_main);

    DrawerLayout drawer_layout = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);

    navController = Navigation.findNavController(this, R.id.nav_host_fragment);

    appBarConfiguration = new AppBarConfiguration.Builder(R.id.homeFragment, R.id.Fragment1,
                          R.id.Fragment2, R.id.Fragment3).setDrawerLayout(drawer_layout).build();

    NavigationUI.setupWithNavController(navigationView, navController);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

@Override
public boolean onSupportNavigateUp() {
    navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, appBarConfiguration) || super.onSupportNavigateUp();
}

nav_graph.xml

<?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/nav_graph"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="package.fragments.homeFragment"
        android:label="@string/home_fragment_label"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/Fragment1"
        android:name="package.fragments.Fragment1"
        android:label="@string/fragment1_label"
        tools:layout="@layout/fragment1" />
    <fragment
        android:id="@+id/Fragment2"
        android:name="package.fragments.Fragment2"
        android:label="@string/fragment2_label"
        tools:layout="@layout/fragment2" />
    <fragment
        android:id="@+id/Fragment3"
        android:name="package.fragments.Fragment3"
        android:label="@string/fragment3_label"
        tools:layout="@layout/fragment3" />
</navigation>

当从Fragment1切换到Fragment2并按回去时,我希望该应用程序返回到Fragment1,但是该应用程序将重定向到homeFragment。 有什么方法可以防止导航组件始终返回到起始片段,而不是前一个片段?

非常感谢

1 个答案:

答案 0 :(得分:0)

我设法产生了所需的行为。我只是忽略了文档的相关部分。如documentation of the NavigationUI中所述,您需要在导航视图菜单中的菜单项中添加ERROR D:\DESARROLLO\menuon-project\be-server\node_modules\mongodb\lib\utils.js:132 throw err; ^ Error: example at D:\DESARROLLO\menuon-project\be-server\users\user.service.js:88:23 at D:\DESARROLLO\menuon-project\be-server\node_modules\mongoose\lib\model.js:4690:16 at model.Query.Query._completeOne (D:\DESARROLLO\menuon-project\be-server\node_modules\mongoose\lib\query.js:1932:12) at cb (D:\DESARROLLO\menuon-project\be-server\node_modules\mongoose\lib\query.js:3285:11) at D:\DESARROLLO\menuon-project\be-server\node_modules\mongoose\lib\query.js:3376:14 at D:\DESARROLLO\menuon-project\be-server\node_modules\mongoose\lib\query.js:4093:12 at result (D:\DESARROLLO\menuon-project\be-server\node_modules\mongodb\lib\utils.js:414:17) at session.endSession (D:\DESARROLLO\menuon-project\be-server\node_modules\mongodb\lib\utils.js:401:11) at ClientSession.endSession (D:\DESARROLLO\menuon-project\be-server\node_modules\mongodb-core\lib\sessions.js:129:41) at executeCallback (D:\DESARROLLO\menuon-project\be-server\node_modules\mongodb\lib\utils.js:397:17) at handleCallback (D:\DESARROLLO\menuon-project\be-server\node_modules\mongodb\lib\utils.js:128:55) at executeCommand (D:\DESARROLLO\menuon-project\be-server\node_modules\mongodb\lib\operations\collection_ops.js:558:12) at handleCallback (D:\DESARROLLO\menuon-project\be-server\node_modules\mongodb\lib\utils.js:128:55) at db.s.topology.command (D:\DESARROLLO\menuon-project\be-server\node_modules\mongodb\lib\operations\db_ops.js:516:5) at D:\DESARROLLO\menuon-project\be-server\node_modules\mongodb-core\lib\connection\pool.js:532:18 at process._tickCallback (internal/process/next_tick.js:61:11) Emitted 'error' event at: at D:\DESARROLLO\menuon-project\be-server\node_modules\mongoose\lib\model.js:4692:13 at model.Query.Query._completeOne (D:\DESARROLLO\menuon-project\be-server\node_modules\mongoose\lib\query.js:1932:12) [... lines matching original stack trace ...] at process._tickCallback (internal/process/next_tick.js:61:11) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-mongo-registration-login-api@1.0.0 start: `node ./server.js` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the node-mongo-registration-login-api@1.0.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\Franco\AppData\Roaming\npm-cache\_logs\2019-08-10T16_55_39_844Z-debug.log 。通过在XML文件中添加此行,在切换到相应的Fragment时不会弹出堆栈,因此应用程序在按回时会返回到上一个Fragment。