android - FragmentTransaction Replace only works once -


iam working on page have 3 buttons working tabs, , when clicking on 1 of them behavior expected framelayout (fragment container) below should replace fragment expected one.

enter image description here

in oncreate of activity have method, container shows first fragment, (this works fine):

productdetailinformacoesfragment infofragment = new productdetailinformacoesfragment(); fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); ft.replace(r.id.product_detail_fragment_container, infofragment); 

then have switch statement called on different listeners buttons, right fragment shown:

public void switchfragment(int fragmentpos) {     switch (fragmentpos){         case 0:             productdetailinformacoesfragment infofragment = new productdetailinformacoesfragment();             fragmenttransaction ft = getsupportfragmentmanager().begintransaction();             ft.replace(r.id.product_detail_fragment_container, infofragment);             ft.commit();             break;          case 1:             productdetailcaracteristicasfragment caracteristicasfragment = new productdetailcaracteristicasfragment();             fragmenttransaction ftt = getsupportfragmentmanager().begintransaction();             ftt.replace(r.id.product_detail_fragment_container, caracteristicasfragment);             ftt.commit();             break;         case 2:             productdetailavaliacoesfragment avaliacoesfragment = new productdetailavaliacoesfragment();             fragmenttransaction fttt = getsupportfragmentmanager().begintransaction();             fttt.replace(r.id.product_detail_fragment_container, avaliacoesfragment);             fttt.commit();             break;         default:             break;     } } 

xml:

            <framelayout             android:id="@+id/product_detail_fragment_container"             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:layout_below="@+id/base_view"             android:background="@android:color/white"/> 

the problem is when click on 1 of tabs, fragment container become blank (white) , desired fragment not showed, no matter 1 click (even 1 shown). what doing wrong?? - have tried setting ft.addtobackstack(null) didn't help.

.you should control if frament null before instanciate , add container. if fragment instanciated should show it. if button clicked manage show desired fragment using trick mentioned above, , hide other fragments. should use actionbar feature of android. , use tab instead of button. ontabselected() ontabunselected() respectively manage operations on tab attach or detach fragments. detailled in android developper forum. here url http://developer.android.com/guide/topics/ui/actionbar.html go section "adding navigation tabs" full explanation example.

here overview of code managing selection , unselection of tabs using androidsupport

public void ontabselected(tab tab, fragmenttransaction ft) {     fragmenttransaction ft = getsupportfragmentmanager().begintransaction();     switch (tab.getposition()) {     case your_tab_position:         if(your_fragment == null){             your_fragment = new your_fragment();             ft.add(r.id.your_container, your_fragment, your_tag);         }         else{             ft.show(your_fragment);             }          break; . . . } }  public void ontabunselected(tab tab, fragmenttransaction ft) {     fragmenttransaction ft = getsupportfragmentmanager().begintransaction();      switch (tab.getposition()) {     case your_tab_position:         if(your_fragment != null){             ft.hide(your_fragment);         }         break; . . . } } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -