android - How can I identify the current fragment of my activity while using tabs and fragment container? -
i want call fragment method activity, describe in so-question: findbyid
testfragment testfrag = (testfragment) getfragmentmanager().getfragmentbyid(r.id.testfragment); if(testfrag != null && testfrag.isadded()) testfrag.testmethod("test");
as seen identify fragment id set in xml. have fragment container none of fragment layouts have id.
so seconds thought using function getfragmentbytag
`like in tags
fragmenttransaction.replace(r.id.frametitle, casinodetailfragment, "fragmenttag");
- but use action bar tabs don't have tags:
my activity:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_fragmentcontainer); // actionbar gets initiated actionbar actionbar = getactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); // define tabs actionbar.tab basicinfotab = actionbar.newtab().settext( r.string.tab_baseinfo); .. // define fragments plantbasicinfofragment basicinfofragment = new plantbasicinfofragment(); .. // set tablisteners basicinfotab.settablistener(new tablistener(basicinfofragment)); .. // add tabs actionbar actionbar.addtab(basicinfotab); .. // icon clickable actionbar.setdisplayhomeasupenabled(true); }
and layout:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center"> <linearlayout android:orientation="horizontal" android:id="@+id/labeledobjectheaderinfo" android:layout_width="match_parent" android:layout_height="wrap_content" > <!-- ... --> </linearlayout > <!-- fragment container --> <linearlayout android:orientation="horizontal" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" > </linearlayout> </linearlayout>
one of fragments:
<?xml version="1.0" encoding="utf-8"?> <tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:id="@+id/table"> <tablerow> <!-- ... --> </tablerow> <!-- ... --> </tablelayout>
and tab listener
public class tablistener implements actionbar.tablistener { public fragment fragment; public tablistener(fragment fragment) { this.fragment = fragment; } @override public void ontabreselected(tab tab, fragmenttransaction ft) { } @override public void ontabselected(tab tab, fragmenttransaction ft) { ft.replace(r.id.fragment_container, fragment); } @override public void ontabunselected(tab tab, fragmenttransaction ft) { ft.remove(fragment); } }
so how can identify current fragment?
thanks in advance
p.s. whole tab switching works perfectly, want build addional feature inside activity!
add tag while creating tabs.
actionbar.tab basicinfotab = actionbar.newtab().settext(r.string.tab_baseinfo); basicinfotab.settag("tab1");
and in listener class, retrieve tag upon event.
@override public void ontabselected(tab tab, fragmenttransaction ft) { string tabid = tab.gettag().tostring(); }
this way current tag. identify current fragment, can keep stack of fragments , pick right 1 on tab change.
edit
the tabid, aforementioned, identifies tab which. in order set fragment id/tag, there many ways depending on needs. 1 method replace actual fragment layout intermediate layout.
for instance, fragment want add fragment container. create fragment class called intermediatefragment following layout.
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:baselinealigned="false"> <fragment android:id="@+id/fragmentid" android:layout_width="match_parent" android:layout_height="match_parent" class="classa" /> </linearlayout>
now assign layout mentioned in post (containing table) new 1 , add fragment class a. add intermediate fragment container in turn pulls fragment a. identify fragment id/tag added in layout.
hope makes sense!
Comments
Post a Comment