android - Fragment is not removed from BackStack -


i use backstack store fragments , works fine. when want remove fragment backstack, nothing happens. checked questions this , saw remove item backstack using popbackstack, me, not work me.

this code:

    fragmentmanager fragmentmanager = getsupportfragmentmanager();     fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction();     int count = getsupportfragmentmanager().getbackstackentrycount();      string currenttag = getsupportfragmentmanager().getbackstackentryat(getsupportfragmentmanager().getbackstackentrycount() - 1).getname();     fragment currentinstance = getsupportfragmentmanager().findfragmentbytag(currenttag);     fragmenttransaction.remove(currentinstance);     fragmenttransaction.commit();     fragmentmanager.popbackstack(getsupportfragmentmanager().getbackstackentrycount() - 1, fragmentmanager.pop_back_stack_inclusive);     int count1 = getsupportfragmentmanager().getbackstackentrycount(); 

currentinstance shows topmost fragment correctly. interestingly count , count1 equals , topmost item in stack remains after using popbackstack command. cant remove topmost fragment stack.

if use code:

        string previoustag = getsupportfragmentmanager().getbackstackentryat(getsupportfragmentmanager().getbackstackentrycount() - 1).getname();         fragment previousinstance = getsupportfragmentmanager().findfragmentbytag(previoustag);          fragmenttransaction.replace(r.id.container_body, previousinstance,previoustag);         fragmenttransaction.commit(); 

i replace fragment previous one, it's not correct way that, because can't more 1 fragment.

i found out: popbackstack method not pop after call. in have use popbackstackimmediate method instead of popbackstack. final corrected code:

fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); if (getsupportfragmentmanager().getbackstackentrycount()> 0) {    fragmentmanager.popbackstackimmediate(getsupportfragmentmanager().getbackstackentrycount() - 1, fragmentmanager.pop_back_stack_inclusive);     string previoustag = getsupportfragmentmanager().getbackstackentryat(getsupportfragmentmanager().getbackstackentrycount() - 1).getname();    fragment previousinstance = getsupportfragmentmanager().findfragmentbytag(previoustag);    fragmenttransaction.replace(r.id.container_body, previousinstance, previoustag);   fragmenttransaction.commit(); } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -