actionbarsherlock - Android action bar tabs: screen rotation -


hi i'm having problems action bar , tabs using abs. have 2 tabs, first one, "favourites", extends fragment , not show anything. second one, "lines", extends sherlocklistfragment , implements loadermanager.loadercallbacks (loads user list database).

the app works ok (or @ least, think so), problem appears when rotate screen. if in portrait mode linestab selected (the list displayed) , change landscape, content of favouritestab (initially empty) filled content of linestab.

this important part of code.

main.java (create both tabs)

public class main extends sherlockfragmentactivity { @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     actionbar actionbar = getsupportactionbar();     actionbar.setnavigationmode(actionbar.navigation_mode_tabs);     actionbar.setdisplayshowtitleenabled(true);      tab tab = actionbar             .newtab()             .settext("favourites")             .settablistener(new favouritestab());     actionbar.addtab(tab);      tab = actionbar             .newtab()             .settext("lines")             .settablistener(new linestab());     actionbar.addtab(tab);     } } 

linestab.java (the tab load users database using loadermanager)

public class linestab extends sherlocklistfragment implements actionbar.tablistener,     loadermanager.loadercallbacks<arraylist<user>> { useradapter adapter; database db;  public void oncreate(bundle savedinstancestate) {     adapter = new useradapter(getactivity(), new arraylist<user>());     setlistadapter(adapter);     getactivity().getsupportloadermanager().initloader(0, null, this);     super.oncreate(savedinstancestate); }  @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle state) {     return super.oncreateview(inflater, container, state); }  @override public void onactivitycreated(bundle state) {     super.onactivitycreated(state); }  // tablistener callbacks public void ontabselected(actionbar.tab tab, fragmenttransaction ft) {     ft.add(android.r.id.content, this, "linestabtag");     ft.attach(this); }  public void ontabunselected(actionbar.tab tab, fragmenttransaction ft) {     ft.detach(this); }  public void ontabreselected(actionbar.tab tab, fragmenttransaction ft) {}  // loadercallbacks @override public loader<arraylist<user>> oncreateloader(int id, bundle args) {     db = new database(getactivity());     return new userloader(getactivity(), db); }  @override public void onloadfinished(loader<arraylist<user>> loader,                            arraylist<user> usuarios) {     ((useradapter) getlistadapter()).update(usuarios);     ((useradapter) getlistadapter()).notifydatasetchanged(); }  @override public void onloaderreset(loader<arraylist<user>> loader) {     setlistadapter(null); } } 

favouritestab.java (the other tab, not show anything)

public class favouritestab extends fragment implements actionbar.tablistener { public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate); }  @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle state) {     return super.oncreateview(inflater, container, state); }  // tablistener callbacks public void ontabselected(actionbar.tab tab, fragmenttransaction ft) {     ft.add(android.r.id.content, this, "linestabtag");     ft.attach(this); }  public void ontabunselected(actionbar.tab tab, fragmenttransaction ft) {     ft.detach(this); }  public void ontabreselected(actionbar.tab tab, fragmenttransaction ft) {} } 

furthermore, i'm not quite sure if code implemented in correct way. ontabselected method. in android documentation implement listener manages fragments, i'm implementing listener in every fragment. don't know if ft.add , ft.attach best way implement method.

thanks.

finally i've found solution. i've implemented general tablistener in main.java , save current tab index everytime screen rotated:

main.java

public class main extends sherlockfragmentactivity { int pos = 0;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     actionbar actionbar = getsupportactionbar();     actionbar.setnavigationmode(actionbar.navigation_mode_tabs);     actionbar.setdisplayshowtitleenabled(true);      if (savedinstancestate != null) {         // restore last tab position         pos = savedinstancestate.getint("pos");     }      tab tab = actionbar             .newtab()             .settext("favourites")             .settablistener(new tablistener<favouritestab>(this, "favourites", favouritestab.class));             //.settablistener(new favouritestab());     actionbar.addtab(tab);      tab = actionbar             .newtab()             .settext("lines")             .settablistener(new tablistener<linestab>(this, "linestab", linestab.class));             //.settablistener(new linestab());     actionbar.addtab(tab);      // set last tab position, or 0 default     getsupportactionbar().setselectednavigationitem(pos); }  @override protected void onsaveinstancestate(bundle outstate) {     // save current tab position     super.onsaveinstancestate(outstate);     outstate.putint("pos", getsupportactionbar().getselectednavigationindex()); }  // tablistener static class tablistener<t extends fragment> implements actionbar.tablistener {     private fragment mfragment;     private sherlockfragmentactivity mactivity;     private string mtag;     private class<t> mclass;      public tablistener(sherlockfragmentactivity activity, string tag, class<t> clz) {         mactivity = activity;         mtag = tag;         mclass = clz;     }      public void ontabselected(tab tab, fragmenttransaction ft) {         // check if fragment initialized         if (mfragment == null) {             // if not, instantiate , add activity             mfragment = fragment.instantiate(mactivity, mclass.getname());             ft.add(android.r.id.content, mfragment, mtag);         } else {             // if exists, attach on order show             ft.attach(mfragment);         }     }      public void ontabunselected(tab tab, fragmenttransaction ft) {         if (mfragment != null) {             ft.detach(mfragment);         }     }      public void ontabreselected(tab tab, fragmenttransaction ft) {} } } 

i've removed loader callbacks in linestab.java , favouritestab.java


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -