android - Tabs content is visible on top of another after screen rotation -
i have read alot on stackoverflow on fragments issues cant find solution problem.
i have tabhost , when change rotation of device , select tab, view first tab visible. both tabs content on top of each other.
i'm using a custom tablistener , every tab fragment. bypass android:configchanges="keyboardhidden|orientation|screensize"
but solution gave me list of other problems , read bad solution.
public class tablistener<t extends fragment> implements actionbar.tablistener { private fragment fragment; private final fragmentactivity activity; private final string tag; private final class<t> myclass; private long id; public tablistener(fragmentactivity a, string t, class<t> c, long id) { tag = t; myclass = c; activity = a; this.id = id; } /** following each of actionbar.tablistener callbacks */ public void ontabselected(tab tab, fragmenttransaction ft) { // check if fragment initialized if (fragment == null) { fragment = fragment.instantiate(activity, myclass.getname()); // sends stored timerclass id fragment if(id != 0) { bundle b = new bundle(); b.putlong("id", id); fragment.setarguments(b); } ft.add(android.r.id.content, fragment, tag); } else { // if exists, attach in order show ft.attach(fragment); } } public void ontabunselected(tab tab, fragmenttransaction ft) { if (fragment != null) ft.detach(fragment); } public void ontabreselected(tab tab, fragmenttransaction ft) { editnamedialog(); } }
i dont know should handled in fragment, activity or in tablistener. tabs content viewd correctly until change screens orientation.
i found parts of answer here on stackoverflow.
solution
this how solved problem. in tab listeners put:
fragment = activity.getsupportfragmentmanager().findfragmentbytag(tag);
in both ontabselect()
, ontabunselect()
select correct tab after orientation change
when screen rotates activity recreated(?) need store last selected tab index. save last selected tab (this goes in activity):
@override protected void onsaveinstancestate(bundle savedinstancestate) { super.onsaveinstancestate(savedinstancestate); savedinstancestate.putint("lasttab", actionbar.getselectednavigationindex()); }
to load tab index, put in oncreate()
of activity, way retreive last tab index , selects it:
if (savedinstancestate != null) { actionbar.selecttab(actionbar.gettabat(savedinstancestate.getint("lasttab"))); }
initialize controllers in fragment
to prevent controls diplay , behave odd moved controls inits in fragment oncreateview()
this:
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.timer, container, false); initializecontrols(v); setseekbars(v); return v; }
i hope else out there.
Comments
Post a Comment