android - FragmentTabHost triggers onCreateView even though fragment is hidden -
i have fragmenttabhost in fragment. have 4 tabs. when click tabs, tab's oncreateview triggered though use fragment transaction hide it?
i debugged , found when click create tab home tab
createfragment createfragment = (createfragment) fm.findfragmentbytag(create_tag);
is null though added in tabhost.addtab?
how can move between tabs , not call oncreateview of each fragments?
thank you
this fragment class
public class tabfragment extends fragment { public static final string home_tag = "home"; public static final string create_tag = "create"; public static final string search_tag = "search"; public static final string profile_tag = "profile"; private myfragmenttabhost tabhost; private myfragmenttabhost.tabinfo tabinfo; @override public view oncreateview(layoutinflater inflater, viewgroup viewgroup, bundle savedinstancestate){ view view = inflater.inflate(r.layout.fragment_tab, viewgroup, false); tabhost = (myfragmenttabhost) view.findviewbyid(android.r.id.tabhost); tabhost.setup(getactivity(), getchildfragmentmanager(), android.r.id.tabcontent); tabhost.addtab(tabhost.newtabspec(home_tag).setindicator(getstring(r.string.tab_home)), homefragment.class, null); tabhost.addtab(tabhost.newtabspec(create_tag).setindicator(getstring(r.string.tab_create)), createfragment.class, null); tabhost.addtab(tabhost.newtabspec(search_tag).setindicator(getstring(r.string.tab_search)), createfragment.class, null); tabhost.addtab(tabhost.newtabspec(profile_tag).setindicator(getstring(r.string.tab_profile)), createfragment.class, null); myfragmenttabhost.ontabchangelistener tablistener = new myfragmenttabhost.ontabchangelistener() { @override public void ontabchanged(string s) { fragmentmanager fm = getchildfragmentmanager(); homefragment homefragment = (homefragment) fm.findfragmentbytag(home_tag); createfragment createfragment = (createfragment) fm.findfragmentbytag(create_tag); fragmenttransaction ft = fm.begintransaction(); if(s.equalsignorecase(home_tag)){ if(homefragment == null) ft.add(r.id.tabcontent, new homefragment(), home_tag); else{ if(createfragment != null) ft.hide(createfragment); ft.show(homefragment); } } else if(s.equalsignorecase(create_tag)){ if(createfragment == null) ft.add(r.id.tabcontent, new createfragment(), create_tag); else{ if(homefragment != null) ft.hide(homefragment); ft.show(createfragment); } } ft.commit(); } }; tabhost.setontabchangedlistener(tablistener); return view; } }
in fragmenttabhost:
private fragmenttransaction dotabchanged(string tabid, fragmenttransaction ft) { tabinfo newtab = null; (int i=0; i<mtabs.size(); i++) { tabinfo tab = mtabs.get(i); if (tab.tag.equals(tabid)) { newtab = tab; } } if (newtab == null) { throw new illegalstateexception("no tab known tag " + tabid); } if (mlasttab != newtab) { if (ft == null) { ft = mfragmentmanager.begintransaction(); } if (mlasttab != null) { if (mlasttab.fragment != null) { ft.detach(mlasttab.fragment); } } if (newtab != null) { if (newtab.fragment == null) { newtab.fragment = fragment.instantiate(mcontext, newtab.clss.getname(), newtab.args); ft.add(mcontainerid, newtab.fragment, newtab.tag); } else { ft.attach(newtab.fragment); } } mlasttab = newtab; } return ft; }
transaction.attach(fragment) re-create view ,see details in source code. so,you can in fragment add code :
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { if (!iscreated || mrootview == null) { iscreated = true; mrootview = inflater.inflate(r.layout.fragment_home_task, container, false); butterknife.bind(this,mrootview); onviewinitialized(); } return mrootview; }
Comments
Post a Comment