android - AsyncTask starts again on tab change with SherlockFragments -
i have mainactivity extends sherlockfragmentactivity , 2 tabs sherlockfragment. these 2 tabs have listview populated asynctask. works fine whenever switch tab tab, asynctask triggers again , want start once.
here mainactivity:
public class mainactivity extends sherlockfragmentactivity { private fragmenttabhost mtabhost; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtabhost = (fragmenttabhost) findviewbyid(android.r.id.tabhost); mtabhost.setup(this, getsupportfragmentmanager(), r.id.tabcontent); mtabhost.addtab(mtabhost.newtabspec("tab1").setindicator("tab1"),tab1.class, null); mtabhost.addtab(mtabhost.newtabspec("tab2").setindicator("tab2"),tab2.class, null); }
and here tab1:
public class tab1 extends sherlockfragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.tab1, container, false); return v; } @override public void onstart() { super.onstart(); // todo auto-generated method stub new fetchdata().execute(); } public class fetchdata extends asynctask<string, string, string> { ..... } }
tab2 contains similar code, different mysql select. works fine, data populated on listview each time switch tab1 tab2, asynctask triggers again :/ please ...
the onstart method called each time fragment become visible, e.g. when switch tabs
source: android fragment life cycle
i suggest move:
new fetchdata().execute();
into oncreateview method.
Comments
Post a Comment