java - Navigating back to a viewpager fragment in a nested fragment -
i have viewpagercontainer fragment loads when app starts. viewpagercontainer fragment adds 2 tabs(tab , tab b) actionbar. tab b has listview of 2 items.
what have done: attached click listener list view item in tab b fragment such when user clicks on item opens fragment (child fragment) inside first fragment i.e under tab b.
where got stuck: i'd go tab b when user presses button. tried implementing adding child fragment backstack, unfortunately not go tab b fragment. kindly me out have done alot of research on including implementing backstack hacks no success.
i using v4 , v7 appcompat libraries.
any me appreciated. in advance.
edited: managed navigate child fragment actionbar tabs. achieved through following code:
//used getchildfragmentmanager() instead of getsupportfragmentmanager() fragmentmanager fm = getchildfragmentmanager();
myfragmentpageradapter fragadapter = new myfragmentpageradapter(fm);
viewpager.setadapter(fragadapter);
however, on navigation number of tabs double , 2 of them clickable , can see content. when click on listitem on second tab again navigate back, 2 more tabs added , goes on , on. how can solve this? here code
tab b fragment: public class findstylist extends listfragment {
string[] selectcat = { "category a", "category b" }; private listview listview; string mtime; public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){
arrayadapter myadapter = new arrayadapter(inflater.getcontext(), android.r.layout .simple_list_item_1, selectcat){
public view getview(int position, view convertview, viewgroup parent){ view view = super.getview(position, convertview, parent); textview tv = (textview) view.findviewbyid(android.r.id.text1); tv.settextcolor(color.black); tv.setgravity(gravity.center); return view; //listview = view.getlistview(); } }; setlistadapter(myadapter);
return super.oncreateview(inflater, container, savedinstancestate);
}
public void onviewcreated(view view, bundle savedinstancestate){
listview = getlistview(); listview.setonitemclicklistener(new onitemclicklistener(){ public void onitemclick(adapterview<?> parent, view view, int position, long id){ if(position == 0){ //best clicked toast.maketext(getactivity(), " cata clicked " + position, 5).show(); }else if(position == 1){ //nearest clicked toast.maketext(getactivity(), "catb clicked " + position, 5).show(); fragmentnearest fragment = new fragmentnearest(); getactivity().getsupportfragmentmanager().begintransaction() .replace(r.id.maincontent, fragment) .addtobackstack(null) .commit(); } } }); }
}
child fragment of b:
public class fragmentnearest extends listfragment{
string[] selectlocation = {"from current location", "choose location"};
private listview listview;
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){
arrayadapter myadapter = new arrayadapter(inflater.getcontext(), android.r.layout .simple_list_item_1, selectlocation){
public view getview(int position, view convertview, viewgroup parent){ view view = super.getview(position, convertview, parent); textview tv = (textview) view.findviewbyid(android.r.id.text1); tv.settextcolor(color.black); tv.setgravity(gravity.center); return view; } }; setlistadapter(myadapter); return super.oncreateview(inflater, container, savedinstancestate); }
you should override onbackpressed method in activity class this.
@override public void onbackpressed() { if (icanbacktofragment) { popbackstackorwhateveryouusetoback() } else { super.onbackpressed(); } }
Comments
Post a Comment