android - restore fragment in viewpager FragmentStatePagerAdapter -


i using viewpager fragmentstatepageradapter (which has many pages) causing memory issues.

this works well, i'd able restore pages (listfragments) original position when fragment recreated. seems straightforward i'm not having luck achieving it.

does have tips?

@override public fragment getitem(int position) {     // todo auto-generated method stub      fragment fragment = new postlistfragment();     bundle args = new bundle();     map<string, string> categoryitem=categorylist.get(position);     args.putstring("slug", categoryitem.get("slug"));     fragment.setarguments(args);     return fragment; } 

in postlistfragment calling web services posts based on "slug".

@override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     // inflate layout fragment     bundle bundle=this.getarguments();     slug=bundle.getstring("slug");      log.v("postlist fragment of"+slug,"oncreateview");      getposts(slug);      view view=inflater.inflate(r.layout.fragment_post_list, container, false);     progressdialog = (progressbar)view.findviewbyid(r.id.progressbar);     postrecyclerview=(recyclerview) view.findviewbyid(r.id.postrecyclerview);     postrecyclerview.setlayoutmanager(new linearlayoutmanager(getactivity()));     postrecyclerview.removeallviews();     return view; } 

here every time webservice being called. want call webservice once specific slug , store slug's posts , reuse posts without calling webservice again.

public class mainfragment extends fragment {  homepageradapter pageradapter; slidingtablayout slidingtabs; viewpager viewpager; mydb db; public mainfragment() {     // required empty public constructor }  @override public void oncreate(@nullable bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setretaininstance(true);     pageradapter=new homepageradapter(getchildfragmentmanager() ,getactivity()); }  @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     // inflate layout fragment     view view=inflater.inflate(r.layout.fragment_main, container,false);      slidingtabs=(slidingtablayout) view.findviewbyid(r.id.sliding_tabs);     viewpager=(viewpager) view.findviewbyid(r.id.vppager);      viewpager.setadapter(pageradapter);     viewpager.setcurrentitem(0);     slidingtabs.setcustomtabview(r.layout.custom_tab, r.id.title);      db=new mydb(getactivity());     slidingtabs.setcustomtabcolorizer(new slidingtablayout.tabcolorizer() {          @override         public int getindicatorcolor(int position) {             // todo auto-generated method stub             return getresources().getcolor(r.color.red);         }     });      slidingtabs.setviewpager(viewpager);     return view; } 

}

my fragmentpageradapter class

public class homepageradapter extends fragmentstatepageradapter {  context context; private sharedpreferences mysharedpref; string userid; mydb db; fragmentmanager mfragmentmanager;  arraylist<map<string, string>> categorylist = new arraylist<map<string, string>>();  public homepageradapter(fragmentmanager fm, context context) {     super(fm);     this.context=context;     this.mfragmentmanager = fm;     mysharedpref = preferencemanager.getdefaultsharedpreferences(context);     userid=mysharedpref.getstring("userid", "0");     db=new mydb(context);     categorylist = db.getcategoryfromdb("30"); }  @override public fragment getitem(int position) {     // todo auto-generated method stub      fragment fragment = new postlistfragment();     bundle args = new bundle();     map<string, string> categoryitem=categorylist.get(position);     args.putstring("slug", categoryitem.get("slug"));     fragment.setarguments(args);     return fragment; }  @override public int getcount() {     // todo auto-generated method stub     return categorylist.size(); }  @override public charsequence getpagetitle(int position) {     // todo auto-generated method stub      //  return super.getpagetitle(position);     map<string, string> categorytitle=categorylist.get(position);     return categorytitle.get("title");      //  return title[position];  } 

}


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -