Android Fragment back stack causing wired issue -
i having fragment in single activity in following sequence.
fragment 1 --> fragment 2 --> fragment 3 --> fragment 4
i using below code fragment transaction.
mfragmenttransaction=mfragmentmanager.begintransaction(); mfragmenttransaction.replace(r.id.fragment_container, mfragment,fragmentname); mfragmenttransaction.addtobackstack(tag); mfragmenttransaction.commit();
what want when user on fragment 3 or 4 on press if user on fragment 4 fragment 4 --> fragment 3 --> fragment 1. if user on fragment 3 fragment 3 --> fragment 1.
i using following code in onback press.
if(mfragmentmanager.findfragmentbytag("fragment 3")!=null){ mfragmentmanager.popbackstack("fragment 2",fragmentmanager.pop_back_stack_inclusive); }else{ super.onbackpressed(); }
but cause wired issue on on press follow.
fragment 4 --> fragment 1 instead of fragment 4 --> fragment 3 --> fragment 1. fragment 3 --> fragment 1
please .
you need have nested if statement. have 1 check if fragment null or not, need check if fragment visible.
if(mfragmentmanager.findfragmentbytag("fragment 3")!=null){ if(mfragmentmanager.findfragmentbytag("fragment 3").isvisible()){ mfragmentmanager.popbackstack("fragment 2",fragmentmanager.pop_back_stack_inclusive); } } else{ super.onbackpressed(); }
Comments
Post a Comment