android - properly remove fragments from FragmentStatePagerAdapter -


hey guys implemented tab layout(an action bar tabs) , made of using pageradapter , fragmentstatepageradapter below code :

public class pageradapter extends fragmentstatepageradapter { int mnumoftabs;  public pageradapter(fragmentmanager fm, int numoftabs) {     super(fm);        this.mnumoftabs = numoftabs; }       @override public fragment getitem(int position) {      switch (position) {         case 0:             tabfragment1 tab1 = new tabfragment1();             return tab1;         case 1:             tabfragment2 tab2 = new tabfragment2();             return tab2;         case 2:             tabfragment3 tab3 = new tabfragment3();             return tab3;         case 3:             tabfragment4 tab4 = new tabfragment4();             return tab4;         case 4:             tabfragment5 tab5 = new tabfragment5();             return tab5;         default:             return null;     } }     @override public void destroyitem(viewgroup container, int position, object object) {     // todo auto-generated method stub     super.destroyitem(container, position, object);     fragmentmanager manager = ((fragment) object).getfragmentmanager();     fragmenttransaction trans = manager.begintransaction();     trans.remove((fragment) object);     trans.commit(); }  @override public int getcount() {     return mnumoftabs; }} 

my mainactivty:

public class mainactivity extends appcompatactivity {    @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);       tablayout tablayout = (tablayout) findviewbyid(r.id.tab_layout);     tablayout.addtab(tablayout.newtab().settext("tab1"));     tablayout.addtab(tablayout.newtab().settext("tab2"));     tablayout.addtab(tablayout.newtab().settext("tab3"));     tablayout.addtab(tablayout.newtab().settext("tab4"));     tablayout.addtab(tablayout.newtab().settext("tab5"));     tablayout.settabgravity(tablayout.gravity_fill);          final viewpager viewpager = (viewpager) findviewbyid(r.id.pager);     final pageradapter adapter = new pageradapter             (getsupportfragmentmanager(), tablayout.gettabcount());     viewpager.setadapter(adapter);     viewpager.addonpagechangelistener(new tablayout.tablayoutonpagechangelistener(tablayout));     tablayout.setontabselectedlistener(new tablayout.ontabselectedlistener() {         @override         public void ontabselected(tablayout.tab tab) {             viewpager.setcurrentitem(tab.getposition());         }          @override         public void ontabunselected(tablayout.tab tab) {          }          @override         public void ontabreselected(tablayout.tab tab) {          }     }); 

now want remove tabs(fragments) , removing them here

        tablayout.addtab(tablayout.newtab().settext("tab1"));         tablayout.addtab(tablayout.newtab().settext("tab2"));         tablayout.addtab(tablayout.newtab().settext("tab3"));         tablayout.addtab(tablayout.newtab().settext("tab4"));      if (condition == true){  // nothing means don't add fragment  }   else {         tablayout.addtab(tablayout.newtab().settext("tab5")); } 

and working not supposed , after removing the tab here tablayout.addtab(tablayout.newtab().settext("tab5")); fragments still there , actively performing task , want shut them , new in android have no idea approach or not guys enough or not please let me know guidance helpful me


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -