我在android中使用新的导航架构组件,我在移动到一个新的片段后被困在清除导航堆栈。

例子: 我在loginFragment中,我想要这个片段从堆栈中清除,当我导航到home片段时,这样用户在按下返回按钮时就不会返回到loginFragment。

我使用一个简单的NavHostFragment.findNavController(Fragment).navigate(R.id.homeFragment)来导航。

当前代码:

mAuth.signInWithCredential(credential)
            .addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        NavHostFragment.findNavController(LoginFragment.this).navigate(R.id.homeFragment);
                    } else {
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                    }
                }
            });

我尝试使用导航()中的NavOptions,但返回按钮仍然将我送回loginFragment

NavOptions.Builder navBuilder = new NavOptions.Builder();
NavOptions navOptions = navBuilder.setPopUpTo(R.id.homeFragment, false).build();   
             NavHostFragment.findNavController(LoginFragment.this).navigate(R.id.homeFragment, null, navOptions);

当前回答

在我的情况下,我需要删除所有在后面的堆栈之前,我打开一个新的片段,所以我使用了这段代码

navController.popBackStack(R.id.fragment_apps, true);
navController.navigate(R.id.fragment_company);

第一行删除back Stack,直到它到达指定的片段,在我的例子中,它是home fragment,所以它完全删除了所有back Stack,当用户点击fragment_company时,他关闭了应用程序。

其他回答

我花了一段时间来防止返回按钮回到我的开始片段,在我的情况下,这是一个只应该出现一次的介绍消息。

简单的解决方案是创建一个全局操作,指向用户应该停留的目的地。你必须正确地设置app:popUpTo="…" -将它设置为你想要弹出的目的地。对我来说,这是我的介绍信息。设置app:popUpToInclusive="true"

    NavController navController 
    =Navigation.findNavController(requireActivity(),          
    R.id.nav_host_fragment);// initialize navcontroller

    if (navController.getCurrentDestination().getId() == 
     R.id.my_current_frag) //for avoid crash
  {
    NavDirections action = 
    DailyInfoSelectorFragmentDirections.actionGoToDestionationFragment();

    //for clear current fragment from stack
    NavOptions options = new 
    NavOptions.Builder().setPopUpTo(R.id.my_current_frag, true).build();
    navController.navigate(action, options);
    }

为Jetpack撰写❤️

navHostController.navigate(Routes.HOME) {
                            this.popUpTo(Routes.ONBOARDING) {
                                this.inclusive = true
                            }
                     }

以下是我如何做到的。

 //here the R.id refer to the fragment one wants to pop back once pressed back from the newly  navigated fragment
 val navOption = NavOptions.Builder().setPopUpTo(R.id.startScorecardFragment, false).build()

//now how to navigate to new fragment
Navigation.findNavController(this, R.id.my_nav_host_fragment)
                    .navigate(R.id.instoredBestPractice, null, navOption)

你可以像这样重写base activity的back pressed:

override fun onBackPressed() {

  val navigationController = nav_host_fragment.findNavController()
  if (navigationController.currentDestination?.id == R.id.firstFragment) {
    finish()
  } else if (navigationController.currentDestination?.id == R.id.secondFragment) {
    // do nothing
  } else {
    super.onBackPressed()
  }

}