我在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);

当前回答

在我的情况下,我使用导航组件与NavigationView(菜单抽屉):

1.

mNavController.popBackStack(R.id.ID_OF_FRAGMENT_ROOT_TO_POP, true)
    mNavController.navigate(
      R.id.DESTINATION_ID,
      null,
      NavOptions.Builder()
                .setPopUpTo(R.id.POP_TO_DESTINATION_ID, true)
                .build()
    )

我想清除堆栈后,点击登出侧菜单抽屉! 希望这能帮助到别人!

其他回答

我终于弄明白了,感谢如何禁用UP在导航的一些片段与新的导航架构组件?

我必须指定. setcleartask (true)作为NavOption。

mAuth.signInWithCredential(credential)
            .addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "signInWithCredential:success");


                        NavOptions.Builder navBuilder = new NavOptions.Builder();
                        NavOptions navOptions = navBuilder.setClearTask(true).build();
                        NavHostFragment.findNavController(LoginFragment.this).navigate(R.id.homeFragment,null,navOptions);
                    } else {
                        Log.w(TAG, "signInWithCredential:failure", task.getException());

                    }

                }
            });

以下是我如何做到的。

 //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)
    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);
    }

使用这段代码

navController.navigateUp();

然后调用新的Fragment Android 4.1.2版本

注意:清除任务已弃用,官方描述为

此方法已弃用。使用setPopUpTo(int, boolean)与NavController的图形的id,并设置包含为true。

旧的答案

如果你不想在代码中使用那些模糊的东西,你可以在动作属性的启动选项中检查Clear Task。

编辑:在Android Studio 3.2 Beta 5中,清除任务不再在启动选项窗口中可见,但你仍然可以在导航的XML代码中使用它,在操作标签中,通过添加

app:clearTask="true"