简体   繁体   中英

Remove fragment from backStack

I have ViewPager that has 3 tabs. Tab(2) includes 2 fragments (A) --> (B). Tab(3) includes 2 fragments (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). My problem is: Tab(2) gets saved in 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. 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")? 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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