how to save the view pager fragment instance in android -


when move tab, every time creating new. getting tabname , catid server,

 private void settabtitle() {    final tablayout  tablayout = (tablayout) findviewbyid(r.id.tabs);     tablayout.settabmode(tablayout.mode_scrollable);  mviewpager.setadapter(new viewpageradapter(getsupportfragmentmanager()));  viewpager.setoffscreenpagelimit(jsontab.size());     tablayout.post(new runnable() {         @override         public void run() {             tablayout.setupwithviewpager(mviewpager);              tablayout.setontabselectedlistener(new tablayout.ontabselectedlistener() {                 @override                 public void ontabselected(tablayout.tab tab) {                     mviewpager.setcurrentitem(tab.getposition());      // current clicked tab position                   }                  @override                 public void ontabunselected(tablayout.tab tab) {                 }                  @override                 public void ontabreselected(tablayout.tab tab) {                 }             });         }     }); } public parcelable savestate() {     return null; } class viewpageradapter extends fragmentstatepageradapter {      public viewpageradapter(fragmentmanager manager) {         super(manager);     }      @override     public fragment getitem(int position) {    return mainfragment.newinstance(catid.get(position), tabname.get(position));   }      @override     public int getcount() {         return tabname.size();     }      @override     public charsequence getpagetitle(int position) {          return tabname.get(position);     } } 

when change in first tab, , go anyother tab, first tab refreshing , creating new instance, everytime calling server fetch details particular tab

try setting viewpager.setofscreenpagelimit(noofpagesinviewpager);

class viewpageradapter extends fragmentstatepageradapter { mainfragment mainfragment;

public viewpageradapter(fragmentmanager manager) {     super(manager);     mainfragment=new mainfragment(); }  @override public fragment getitem(int position) {       return mainfragment(catid.get(position), tabname.get(position)); 

}

@override public int getcount() {     return tabname.size(); }  @override public charsequence getpagetitle(int position) {      return tabname.get(position); } 

}


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -