简体   繁体   English

从 backStack 中删除片段

[英]Remove fragment from backStack

I have ViewPager that has 3 tabs.我有具有 3 个选项卡的 ViewPager。 Tab(2) includes 2 fragments (A) --> (B). Tab(2) 包括 2 个片段 (A) --> (B)。 Tab(3) includes 2 fragments (C) --> (D). Tab(3) 包括 2 个片段 (C) --> (D)。

Tab(1)
Tab(2) --> (A) --> (B) 
Tab(3) --> (C) --> (D)

Inside of (A) when it's created, variable "isUserLogged" being checked (true/false).在 (A) 内部创建时,检查变量“isUserLogged”(真/假)。 My problem is: Tab(2) gets saved in ViewPager backstack.我的问题是:Tab(2) 被保存在 ViewPager backstack 中。 So when I hit a button in (D) I want to remove Tab(2) so when it's created again it would check "isUserLogged" again.因此,当我点击 (D) 中的按钮时,我想删除 Tab(2),这样当它再次创建时,它会再次检查“isUserLogged”。 This is how I replace fragments:这就是我替换片段的方式:

FragmentTransaction trans = getChildFragmentManager().beginTransaction();
        trans.replace(R.id.hostsUlogged_container, new FragmentHostProfile(), "HostUnlogged-HostProfile");
        trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        trans.addToBackStack("hostUnlogged_screen");
        trans.commit();

How do I remove specific fragment with tag ("hostUnlogged_screen")?如何删除带有标签(“hostUnlogged_screen”)的特定片段? Thank you.谢谢你。

You can remove specific fragment by tag as following:您可以按标签删除特定片段,如下所示:

FragmentManager fm = getChildFragmentManager();
Fragment fragment=fm.findFragmentByTag("hostUnlogged_screen");
fm.beginTransaction().remove(fragment).commit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM