我正在使用
Android中的新
Navigation Architecture Component,并且在移动到新片段后我被困在清理导航堆栈中.
例:
我在loginFragment中,当我导航到主片段时,我希望从堆栈中清除这个片段,这样当用户按下后退按钮时,用户将不会返回到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()); } } });
我尝试在navigate()中使用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);