How to clear navigation Stack after navigating to another fragment in Android

问题:

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章