Check any fragment is presented in Layout in android? -


in application.there 5 button . when curresponding button pressed fragment load in linear layout.what want if button pressed current fragment in layout should dettach , fragment corresponding button should placed.i need common method remove placed fragment.i know can remove fragment using id.i dont need that.i want common method remove fragments.here java class.

   button b1, b2, b3, b4, b5;         // declare variables         actionbar mactionbar;         viewpager mpager;         fragmentmanager fm;  setcontentview(r.layout.activity_main);         fm = getsupportfragmentmanager();b1 = (button) findviewbyid(r.id.fbbutton1);         b2 = (button) findviewbyid(r.id.fbbutton2);         b3 = (button) findviewbyid(r.id.fbbutton3);         b4 = (button) findviewbyid(r.id.fbbutton4);         b5 = (button) findviewbyid(r.id.fbbutton5);         b1.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 fragmenttab1 f1 = new fragmenttab1();                 fragmenttransaction ft = fm.begintransaction();                 ft.add(r.id.fragmentgroup, f1, "a");                 ft.commit();              }         });         b2.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 if (getsupportfragmentmanager().findfragmentbyid(r.id.detail_screen) != null) {                      getsupportfragmentmanager().popbackstack();                          } //to add fragment                 fragmenttab2 f2 = new fragmenttab2();                 fragmenttransaction ft = fm.begintransaction(); //              fm.popbackstack(fm.getbackstackentrycount(), fragmentmanager.pop_back_stack_inclusive); //              - use fragmentmanager.getbackstackentrycount()/getbackstackentryat().getid() retrieve id of first entry on stack, , fragmentmanager.popbackstack(int id, fragmentmanager.pop_back_stack_inclusive).  //                ft.replace(r.id.fragmentgroup, f2);                 ft.add(r.id.fragmentgroup, f2, "b");                 ft.commit();              }         });         b3.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 fragmenttab3 f3 = new fragmenttab3();                 fragmenttransaction ft = fm.begintransaction();                 ft.add(r.id.fragmentgroup, f3, "c");                 ft.commit();              }         });         b4.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 fragmenttab4 f4 = new fragmenttab4();                 fragmenttransaction ft = fm.begintransaction();                 ft.add(r.id.fragmentgroup, f4, "d");                 ft.commit();              }         });         b5.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 fragmenttab5 f5 = new fragmenttab5();                 fragmenttransaction ft = fm.begintransaction();                 ft.add(r.id.fragmentgroup, f5, "e");                 ft.commit();              }         }); 

the layout showing fragments in xml

 <linearlayout         android:id="@+id/fragmentgroup"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:layout_weight="1"         android:background="#f2a4a9"         android:orientation="horizontal" > 

you need common method replace fragments corresponding button click. so, can use replace method in fragmenttransaction job in common method.

code be,

public void replaceafragment(int frag_container, fragment afragment, string tag) {     fragmenttransaction transaction =    getsupportfragmentmanager().begintransaction();     transaction.replace(frag_container, afragment, tag);     transaction.commit(); } 

whenever want load fragment, no need remove replace new fragment,

b3.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {             fragmenttab3 f3 = new fragmenttab3();             replaceafragment(r.id.fragmentgroup,f3,"fragment three");          }     });     b4.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {             fragmenttab4 f4 = new fragmenttab4();             replaceafragment(r.id.fragmentgroup,f4,"fragment four");         }     });     b5.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {             fragmenttab5 f5 = new fragmenttab5();             replaceafragment(r.id.fragmentgroup,f5,"fragment five");         }     }); 

now have common method place fragments. may do, detach , remove inside tag.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -