問題:
I am using The new Navigation Architecture Component in android and I am stuck in clearing the navigation stack after moving to a new fragment.我在 android 中使用新的導航架構組件,但在移動到新片段後我一直在清除導航堆棧。
Example: I am in the loginFragment and I want this fragment to be cleared from the stack when I navigate to the home fragment so that the user will not be returned back to the loginFragment when he presses the back button.示例:我在 loginFragment 中,我希望在導航到 home 片段時從堆棧中清除此片段,以便用戶在按下後退按鈕時不會返回到 loginFragment。
I am using a simple NavHostFragment.findNavController(Fragment).navigate(R.id.homeFragment) to navigate.我正在使用一個簡單的NavHostFragment.findNavController(Fragment).navigate(R.id.homeFragment)進行導航。
Current Code :當前代碼:
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());
}
}
});
I tried using the NavOptions in the navigate() , but the back button is still sending me back to the loginFragment我嘗試在navigation () 中使用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);
解決方案:
參考一: https://en.stackoom.com/question/3PxCA參考二: https://stackoom.com/question/3PxCA