java - FragmentManager replace not working -
when use replace function fragmentmanager, fragment being added on top of stack without removing older version. issues i'm able find online stem when people switch between versions of fragmentmanager, can see, use recent version.
case 2: getgmcompapp().changeactivitiesfragment(new itservicecenterlistfragment(), true); break;
-
public void changeactivitiesfragment(fragment frag, boolean addtobackstack, boolean addifsamefragment) { try { if (frag == null) { // incoming fragment null, cancel operation return; }else if(getcurrentactivity() != null){ //activity not null, attempt change activity //grab fragment manager fragmentmanager fragmentmanager = getcurrentactivity().getfragmentmanager(); if(shouldincomingfragmentshow(frag, addifsamefragment)){ //the fragment should swapped out setcurrentfragment(frag); if(!addtobackstack){ //the previous fragment should not added backstack navigation fragmentmanager.begintransaction() .replace(r.id.container, frag).commit(); toast.maketext(getapplicationcontext(), "did not add back", toast.length_short).show(); } else { // add current transaction backstack // easier navigation int backstackcount = fragmentmanager.getbackstackentrycount(); string tag = integer.tostring(backstackcount); //checks see if fragment exists somewhere on backstack for(int = 0; < backstackcount; i++){ //toast.maketext(getapplicationcontext(), "cmp: " + frag.getclass().getsimplename() +" " + fragmentmanager.getbackstackentryat(i).getname(), toast.length_short).show(); if(frag.getclass().getsimplename().equals(fragmentmanager.getbackstackentryat(i).getname())){ //fragment duplicatefragment = fragmentmanager.findfragmentbytag(integer.tostring(i)); //tag = integer.tostring(i); //fragmentmanager.begintransaction().remove(duplicatefragment).commit(); } } fragmentmanager .begintransaction() .replace(r.id.container, frag) .addtobackstack(frag.getclass().getsimplename()) .commit(); } if(frag.getclass().equals(viewimagefragment.class)) { getcurrentactivity().setrequestedorientation(activityinfo.screen_orientation_landscape); }else if(getcurrentactivity().getrequestedorientation() != activityinfo.screen_orientation_portrait){ getcurrentactivity().setrequestedorientation(activityinfo.screen_orientation_portrait); } } } } catch (exception ex) { log.e(tag, ex.getmessage()); } }
the relevant xml:
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- main content view --> <framelayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black"/> <!-- navigation drawer --> <listview android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choicemode="singlechoice" android:divider="@android:color/transparent" android:dividerheight="2dp" android:background="#111"/>
i attempted manually remove , add fragments, , not able remove remove fragment, assume same issue replace (the remove part failing add being successful).
thanks.
i created own solution , posting in case helps else. manually managed fragments , created own stack in array. stores fragments in between new version , old version of duplicate fragment, , pops off duplicate. restores in-between fragments , adds brand new fragment on top.
private void managebackstack(fragment frag){ fragmentmanager fragmanager = getcurrentactivity().getfragmentmanager(); fragment testfrag = fragmanager.findfragmentbytag(frag.getclass().getsimplename()); if(testfrag != null) { //if fragment exists int maxindex = fragmanager.getbackstackentrycount() - 1; fragment[] stackplaceholder = new fragment[maxindex]; //store fragments between new fragment , old duplicate in array (int = maxindex; >= 0; i--) { string newfragname = fragmanager.getbackstackentryat(i).getname(); if(!newfragname.equals(frag.getclass().getsimplename())) { //do not add fragments generated inside other fragments on stack if (!newfragname.equals("itservicecenterfragment")) { stackplaceholder[i - 1] = fragmanager.findfragmentbytag(newfragname); } fragmanager.popbackstackimmediate(); }else { fragmanager.begintransaction().remove(fragmanager.findfragmentbytag(newfragname)).commit(); fragmanager.popbackstackimmediate(); break; } } //push in-between fragments onto stack (int = 0; < maxindex; i++) { if (stackplaceholder[i] != null) { string fragname = stackplaceholder[i].getclass().getsimplename(); fragmanager .begintransaction() .replace(r.id.container, stackplaceholder[i], fragname) .addtobackstack(fragname) .commit(); } } } //push recent fragment onto stack fragmanager .begintransaction() .replace(r.id.container, frag, frag.getclass().getsimplename()) .addtobackstack(frag.getclass().getsimplename()) .commit(); }
Comments
Post a Comment