android - Navigation with Tabhost and viewpager, data lost issue -


i build app sliding menu , has tabs created using viewpager. i'm getting issue. first time when run app, data getting on first screen on tabs. when change screen using sliding menu, takes me other fragment , when come home screen, data on tabs goes away. used setretaininstance(true); store view when change orientation. when change orientation, data appears strangly. not able find problem. here code.

mainactivity

public class mainactivity extends fragmentactivity {     private drawerlayout mdrawerlayout;     private listview mdrawerlist;     private actionbardrawertoggle mdrawertoggle;      // nav drawer title     private charsequence mdrawertitle;      // used store app title     private charsequence mtitle;      // slide menu items     private string[] navmenutitles;     private typedarray navmenuicons;      private arraylist<navdraweritem> navdraweritems;     private navdrawerlistadapter adapter;       viewpager tab;     tabpageradapter tabadapter;     actionbar actionbar;      private fragmenttabhost mtabhost;      private static final string tag_task_fragment = "task_fragment";     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);                      mtitle = mdrawertitle = gettitle();          // load slide menu items         navmenutitles = getresources().getstringarray(r.array.nav_drawer_items);          // nav drawer icons resources         navmenuicons = getresources()                 .obtaintypedarray(r.array.nav_drawer_icons);          mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout);         mdrawerlist = (listview) findviewbyid(r.id.list_slidermenu);          navdraweritems = new arraylist<navdraweritem>();          // adding nav drawer items array         // home         navdraweritems.add(new navdraweritem(navmenutitles[0], navmenuicons.getresourceid(0, -1)));         // find people         navdraweritems.add(new navdraweritem(navmenutitles[1], navmenuicons.getresourceid(1, -1)));         // photos         navdraweritems.add(new navdraweritem(navmenutitles[2], navmenuicons.getresourceid(2, -1)));         // communities, add counter here         navdraweritems.add(new navdraweritem(navmenutitles[3], navmenuicons.getresourceid(3, -1), true, "22"));         // pages         navdraweritems.add(new navdraweritem(navmenutitles[4], navmenuicons.getresourceid(4, -1)));         // what's hot,  add counter here         navdraweritems.add(new navdraweritem(navmenutitles[5], navmenuicons.getresourceid(5, -1), true, "50+"));           // recycle typed array         navmenuicons.recycle();          mdrawerlist.setonitemclicklistener(new slidemenuclicklistener());          // setting nav drawer list adapter         adapter = new navdrawerlistadapter(getapplicationcontext(),                 navdraweritems);         mdrawerlist.setadapter(adapter);          // enabling action bar app icon , behaving toggle button         getactionbar().setdisplayhomeasupenabled(true);         getactionbar().sethomebuttonenabled(true);          mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout,                 r.drawable.ic_drawer, //nav menu toggle icon                 r.string.app_name, // nav drawer open - description accessibility                 r.string.app_name // nav drawer close - description accessibility         ) {             public void ondrawerclosed(view view) {                 getactionbar().settitle(mtitle);                 // calling onprepareoptionsmenu() show action bar icons                 invalidateoptionsmenu();             }              public void ondraweropened(view drawerview) {                 getactionbar().settitle(mdrawertitle);                 // calling onprepareoptionsmenu() hide action bar icons                 invalidateoptionsmenu();             }         };         mdrawerlayout.setdrawerlistener(mdrawertoggle);          if (savedinstancestate == null) {             // on first time display view first nav item             displayview(0);         }       /*//testing code adding sliding fragment           tabadapter = new tabpageradapter(getsupportfragmentmanager());              tab = (viewpager)findviewbyid(r.id.pager);             tab.setonpagechangelistener(                     new viewpager.simpleonpagechangelistener() {                         @override                         public void onpageselected(int position) {                              actionbar = getactionbar();                             actionbar.setselectednavigationitem(position);                    }                     });             tab.setadapter(tabadapter);              actionbar = getactionbar();             //enable tabs on action bar             actionbar.setnavigationmode(actionbar.navigation_mode_tabs);             actionbar.tablistener tablistener = new actionbar.tablistener(){                  @override                 public void ontabreselected(android.app.actionbar.tab tab,                         fragmenttransaction ft) {                     // todo auto-generated method stub                  }                  @override                  public void ontabselected(actionbar.tab tab, fragmenttransaction ft) {                      tab.setcurrentitem(tab.getposition());                 }                  @override                 public void ontabunselected(android.app.actionbar.tab tab,                         fragmenttransaction ft) {                     // todo auto-generated method stub                  }};                 //add new tab                 actionbar.addtab(actionbar.newtab().settext("android").settablistener(tablistener));                 actionbar.addtab(actionbar.newtab().settext("ios").settablistener(tablistener));             //  actionbar.addtab(actionbar.newtab().settext("windows").settablistener(tablistener)); */        }      /**      * slide menu item click listener      * */     private class slidemenuclicklistener implements             listview.onitemclicklistener {         @override         public void onitemclick(adapterview<?> parent, view view, int position,                 long id) {             // display view selected nav drawer item             displayview(position);         }     }      @override     public boolean oncreateoptionsmenu(menu menu) {         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // toggle nav drawer on selecting action bar app icon/title         if (mdrawertoggle.onoptionsitemselected(item)) {             return true;         }         // handle action bar actions click         switch (item.getitemid()) {         case r.id.action_settings:             return true;         default:             return super.onoptionsitemselected(item);         }     }      /* *      * called when invalidateoptionsmenu() triggered      */     @override     public boolean onprepareoptionsmenu(menu menu) {         // if nav drawer opened, hide action items         boolean draweropen = mdrawerlayout.isdraweropen(mdrawerlist);         menu.finditem(r.id.action_settings).setvisible(!draweropen);         return super.onprepareoptionsmenu(menu);     }      /**      * diplaying fragment view selected nav drawer list item      * */     private void displayview(int position) {         // update main content replacing fragments         fragment fragment = null;         switch (position) {         case 0:             fragment = new homefragment();             /*intent = new intent(mainactivity.this,frontactivity.class);             startactivity(i);*/             /*intent firstpage= new intent(mainactivity.this,homefragment.class);             mainactivity.this.startactivity(firstpage);*/  //          toast.maketext(mainactivity.this, "home pressed", toast.length_short).show();             break;         case 1:             fragment = new findpeoplefragment();  //          toast.maketext(mainactivity.this, "find people pressed", toast.length_short).show();     //      fragment = new homefragment();             break;         case 2:             fragment = new photosfragment(); //          toast.maketext(mainactivity.this, "photo fragment pressed", toast.length_short).show();     //      fragment = new homefragment();             break;         case 3:             fragment = new communityfragment(); //          toast.maketext(mainactivity.this, "communities pressed", toast.length_short).show();         //  fragment = new homefragment();             break;         case 4:             fragment = new pagesfragment(); //          toast.maketext(mainactivity.this, "pages pressed", toast.length_short).show();     //      fragment = new homefragment();             break;         case 5:             fragment = new whatshotfragment(); //          toast.maketext(mainactivity.this, "what's hot pressed", toast.length_short).show();     //      fragment = new homefragment();             break;          default:             break;         }          if (fragment != null) {              system.out.println("*****on fragment creation");             fragmentmanager fragmentmanager = getfragmentmanager();             fragmentmanager.begintransaction()                     .replace(r.id.frame_container, fragment).commit();              // update selected item , title, close drawer             mdrawerlist.setitemchecked(position, true);             mdrawerlist.setselection(position);             settitle(navmenutitles[position]);             mdrawerlayout.closedrawer(mdrawerlist);         } else {             // error in creating fragment             log.e("mainactivity", "error in creating fragment");         }     }      @override     public void settitle(charsequence title) {         mtitle = title;         getactionbar().settitle(mtitle);     }      /**      * when using actionbardrawertoggle, must call during      * onpostcreate() , onconfigurationchanged()...      */      @override     protected void onpostcreate(bundle savedinstancestate) {         super.onpostcreate(savedinstancestate);         // sync toggle state after onrestoreinstancestate has occurred.         mdrawertoggle.syncstate();     }      @override     public void onconfigurationchanged(configuration newconfig) {         super.onconfigurationchanged(newconfig);         // pass configuration change drawer toggls         mdrawertoggle.onconfigurationchanged(newconfig);     }  } 

homefragment

public class homefragment extends fragment {     private tabhost mtabhost;      private viewpager mviewpager;      private tabspageradapter mtabsadapter;     public homefragment(){}     @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {         //setretaininstance(true);         view rootview = inflater.inflate(r.layout.fragment_home, container, false);          return rootview;     }       @override     public void onactivitycreated(bundle savedinstancestate) {         // todo auto-generated method stub         super.onactivitycreated(savedinstancestate);         if(savedinstancestate==null)         {             toast.maketext(getactivity(), "onactivitycreated", toast.length_short).show();             setretaininstance(true);             system.out.println("@@@ if condition @@@@");             mtabhost = (tabhost) getactivity().findviewbyid(android.r.id.tabhost);             mtabhost.setup();             log.e("mtabhost", mtabhost + "");              mviewpager = (viewpager) getactivity().findviewbyid(r.id.viewpager);             log.e("mviewpager", mviewpager + "");             mtabsadapter = new tabspageradapter((fragmentactivity) getactivity(), mtabhost, mviewpager, getchildfragmentmanager());              // here load content each tab.              mtabsadapter.addtab(mtabhost.newtabspec("audience").setindicator("android"), android.class, null);             mtabsadapter.addtab(mtabhost.newtabspec("critics").setindicator("ios"), ios.class, null);             log.e("mtabsadapter", mtabsadapter + "");         }         else         {             system.out.println("@@@@@  else condition  @@@@@");         }       }      } 

fragment_home

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent">      <tabhost             android:id="@android:id/tabhost"             android:layout_width="fill_parent"             android:layout_height="fill_parent" >              <linearlayout                 android:layout_width="fill_parent"                 android:layout_height="fill_parent"                 android:orientation="vertical" >                  <framelayout                     android:id="@android:id/tabcontent"                     android:layout_width="0dp"                     android:layout_height="0dp"/>                  <android.support.v4.view.viewpager                     android:id="@+id/viewpager"                     android:layout_width="fill_parent"                     android:layout_height="0dp"                     android:layout_weight="1" />                  <tabwidget                     android:id="@android:id/tabs"                     android:layout_width="fill_parent"                     android:layout_height="wrap_content"                     android:orientation="horizontal" >                 </tabwidget>              </linearlayout>         </tabhost>  </relativelayout> 

tabspageradapter

 public class tabspageradapter extends fragmentpageradapter     implements tabhost.ontabchangelistener, viewpager.onpagechangelistener { private final context mcontext; private final tabhost mtabhost; private final viewpager mviewpager; private final arraylist<tabinfo> mtabs = new arraylist<tabinfo>();  static final class tabinfo {     private final string tag;     private final class<?> clss;     private final bundle args;      tabinfo(string _tag, class<?> _class, bundle _args)     {         tag = _tag;         clss = _class;         args = _args;     } }  static class dummytabfactory implements tabhost.tabcontentfactory {     private final context mcontext;      public dummytabfactory(context context)     {         mcontext = context;     }      public view createtabcontent(string tag)     {         view v = new view(mcontext);         v.setminimumwidth(0);         v.setminimumheight(0);         return v;     } }  public tabspageradapter(fragmentactivity activity, tabhost tabhost, viewpager pager, fragmentmanager fragmentmanager) {     //super(fragmentmanager);     super(activity.getsupportfragmentmanager());     mcontext = activity;     mtabhost = tabhost;     mviewpager = pager;     mtabhost.setontabchangedlistener(this);     mviewpager.setadapter(this);     mviewpager.setonpagechangelistener(this); }   public void addtab(tabhost.tabspec tabspec, class<?> clss, bundle args) {     tabspec.setcontent(new dummytabfactory(mcontext));     string tag = tabspec.gettag();      tabinfo info = new tabinfo(tag, clss, args);     mtabs.add(info);     mtabhost.addtab(tabspec);     notifydatasetchanged(); }  @override public int getcount() {     return mtabs.size(); }  @override public fragment getitem(int position) {     tabinfo info = mtabs.get(position);      return fragment.instantiate(mcontext, info.clss.getname(), info.args);  }  public void ontabchanged(string tabid) {     int position = mtabhost.getcurrenttab();     mviewpager.setcurrentitem(position);  }  public string gettag() {                return mtabhost.getcurrenttabtag(); }  public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) {      int pos = this.mviewpager.getcurrentitem();      this.mtabhost.setcurrenttab(pos); }  public void onpageselected(int position) {     // unfortunately when tabhost changes current tab, kindly     // takes care of putting focus on when not in touch mode.     // jerk.     // hack tries prevent pulling focus out of our     // viewpager.     tabwidget widget = mtabhost.gettabwidget();     int oldfocusability = widget.getdescendantfocusability();     widget.setdescendantfocusability(viewgroup.focus_block_descendants);     mtabhost.setcurrenttab(position);     widget.setdescendantfocusability(oldfocusability); }  public void onpagescrollstatechanged(int state) { } } 

first screen list showing

second screen move different screen

third screen moved

when come screen content empty.

i same problem, able resolve it. think in classes imported class android.app fragment. replace file android.support.v4.app , resolve problem.

replace android.app android.support.v4.app


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -