android - Fragment remains on ViewPager after destroy -


i using fragmentstatepageradapter multiple instances of same fragment populate viewpager. each fragment makes api call using retrofit + rxjava load list items.

when switch tabs after current tab has finished loading items, alright. when switch tab while has not done fetching items yet, content of fragment remains on viewpager though corresponding life-cycle methods including ondestroyview gets called.

mainactivity.java

public class mainactivity extends appcompatactivity {      @bindview(r.id.toolbar) toolbar mtoolbar;     @bindview(r.id.tab_layout) slidingtablayout mtablayout;     @bindview(r.id.view_pager) viewpager mviewpager;      @override     protected void oncreate(@nullable bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         butterknife.bind(this);          setsupportactionbar(mtoolbar);          mainpageradapter adapter = new mainpageradapter(getsupportfragmentmanager(), getresources().getstringarray(r.array.titles));         mviewpager.setadapter(adapter);          mtablayout.setviewpager(mviewpager);     } } 

mainpageradapter.java

public class mainpageradapter extends fragmentstatepageradapter {      private string[] mtitles;      public mainpageradapter(fragmentmanager fm, string[] titles) {         super(fm);         mtitles = titles;     }      @override     public fragment getitem(int position) {         return mainfragment.newinstance(mtitles[position]);     }      @override     public charsequence getpagetitle(int position) {         return mtitles[position];     }      @override     public int getcount() {         return mtitles.length();     } } 

mainfragment.java

public class mainfragment extends fragment {      @bindview(r.id.swipe_refresh) swiperefreshlayout mswiperefresh;     @bindview(r.id.recycler_view) recyclerview mrecyclerview;      private unbinder munbinder;      private itemsadapter madapter;      private compositedisposable mcompositedisposable = new compositedisposable();      private string mtitle;      public static mainfragment newinstance(string title) {         mainfragment fragment = new mainfragment();         bundle args = new bundle();         args.putstring("title", title);         fragment.setarguments(args);         return fragment;     }      @override     public void oncreate(@nullable bundle savedinstancestate) {         super.oncreate(savedinstancestate);         mtitle = getarguments().getstring("title");     }      @nullable     @override     public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) {         return inflater.inflate(r.layout.fragment_list, container, false);     }      @override     public void onviewcreated(view view, @nullable bundle savedinstancestate) {         munbinder = butterknife.bind(this, view);          mswiperefresh.setonrefreshlistener(new swiperefreshlayout.onrefreshlistener() {             @override             public void onrefresh() {                 fetchitems();             }         });          madapter = new itemsadapter();          mrecyclerview.sethasfixedsize(true);         mrecyclerview.setadapter(madapter);         mrecyclerview.setlayoutmanager(new linearlayoutmanager(getactivity()));         mrecyclerview.setitemanimator(new defaultitemanimator());         mrecyclerview.additemdecoration(new divideritemdecoration(getactivity(), divideritemdecoration.vertical));     }      @override     public void onactivitycreated(@nullable bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);         mswiperefresh.setrefreshing(true);         fetchitems();     }      @override     public void ondestroyview() {         super.ondestroyview();         munbinder.unbind();     }      @override     public void ondestroy() {         super.ondestroy();         mcompositedisposable.clear();     }      private void fetchitems() {         requestinterface requestinterface = utils.createservice();         observable<response> observable;         observable = requestinterface.getitems(mtitle);         mcompositedisposable.add(observable             .subscribeon(schedulers.io())             .observeon(androidschedulers.mainthread())             .subscribe(new consumer<response>() {                 @override                 public void accept(@nonnull response response) throws exception {                     madapter.swapitems(response.getposts());                     mswiperefresh.setrefreshing(false);                 }             }, new consumer<throwable>() {                 @override                 public void accept(@nonnull throwable throwable) throws exception {                     toast.maketext(getactivity(), "error: " + throwable.getlocalizedmessage(), toast.length_long).show();                     mswiperefresh.setrefreshing(false);                 }             }));     } } 

to exemplify situation, imagine first tab loading items. there text , icon indicating data loading. while it's loading, switch last tab supposing fragmentstatepageradapter destroys first tab. if switch first tab, initiates , finishes loading of items text , icon previous destroyed fragment on top of screen!


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -