您好,我正在开发一个应用程序,但我卡在导航视图中的导航上。
示例我有活动,在里面我定义了导航主机控制器,如下所示
setSupportActionBar(mMainToolbar);
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.homeFragment, R.id.questionBankFragment,
R.id.testFragment, R.id.dailyHuntFragment,
R.id.liveClassesFragment)
.setOpenableLayout(mMainDrawerLayout)
.build();
View mNavigationViewHeaderView = mNavigationView.getHeaderView(0);
mNavHostFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
mNavController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, mNavController, mAppBarConfiguration);
NavigationUI.setupWithNavController(mNavigationView, mNavController);
NavigationUI.setupWithNavController(mMainToolbar, mNavController, mAppBarConfiguration);
NavigationUI.setupWithNavController(mBottomNavigationView, mNavController);
mNavController.addOnDestinationChangedListener((controller, destination, arguments) -> {
switch (destination.getId()) {
case R.id.homeFragment:
case R.id.questionBankFragment:
mMainToolbar.setVisibility(View.VISIBLE);
mBottomNavigationView.setVisibility(View.VISIBLE);
setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
setDrawerLocked(false);
break;
case R.id.welcomeFragment:
case R.id.loginFragment:
mMainToolbar.setVisibility(View.GONE);
mBottomNavigationView.setVisibility(View.GONE);
setStatusBarColor(Color.WHITE);
setDrawerLocked(true);
break;
case R.id.customModuleFragment:
case R.id.createCustomModuleOneFragment:
case R.id.createCustomModuleTwoFragment:
mBottomNavigationView.setVisibility(View.GONE);
setDrawerLocked(true);
break;
case R.id.logoutFragment:
new AppSharedPreference(MainActivity.this).clearAllData();
Intent intent = new Intent(getApplicationContext(), UserAuthanticationActivity.class);
startActivity(intent);
finish();
break;
}
});
它工作正常。但现在我必须处理我无法理解的后栈导航。
我的问题是假设我是底部导航有 4 个菜单。家庭,狩猎,Qbank,考试。
我被选中了Qbank。Qbank 有 5 个片段 A、B、C、D、E。现在我已经开始遍历片段 A -> B B -> C C -> D
现在我可以选择在哪里导航回到 Direct A 之类的。当前位置是D
D - > A
但是我如何才能回到 A 并清除我遍历 A -> B -> C -> D 的堆栈,我想清除它,就像我想从 A 重新开始一样。
我如何使用 Jetpack 导航组件成为可能。
我尝试了直接导航或弹出堆栈导航,但它不起作用,它给了我错误,就像我直接导航到
java.lang.IllegalStateException: View androidx.core.widget.NestedScrollView{98060fe VFED..... ......ID 0,0-0,0} does not have a NavController set
at androidx.navigation.Navigation.findNavController(Navigation.java:84)
at app.technotech.koncpt.McqTestFragment$2.onClick(McqTestFragment.java:232)
at android.view.View.performClick(View.java:7201)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7170)
at android.view.View.access$3500(View.java:806)
at android.view.View$PerformClick.run(View.java:27562)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
我不知道如何处理这个导航。请帮助我解决这个问题
提前致谢。