android - Fragment still displayed on screen when back key while refreshing with SwipeRefreshLayout -


i have activity 2 fragments: , b. added on activity. after click button:

ft.replace(containerid, b, tag); ft.addtobackstack("a"); ft.commit(); 

b displayed , removed normally. pull refresh , press while progress loading:

// implementation in onbackpressed() .... int backstackentrycount = fm.getbackstackentrycount(); if (backstackentrycount > 0) {     fm.popbackstackimmediate(); } .... 

the problem fragment b still displayed on screen (blank) , overlap fragment a, if again app exit. information b:

  • i implemented swiperefreshlayout on b
  • here b source code:

    public class b extends myfragment {  protected view view; protected listview listview; protected imageview progress; protected textview notiftext; protected layoutinflater inflater; protected ptrbaseadapter adapter; protected swiperefreshlayout mswiperefreshlayout;  protected string emptytext; protected boolean fromrest; protected int loads = 0; protected float mpreviousx = 0; protected float mpreviousy = 0;  protected void dorefresh(boolean fromrest) {     if (!isadded()) return;     notiftext.setvisibility(view.gone);     fetchfromdatabase();     if (fromrest) {         refresh();     } }  private void fetchfromdatabase() {     /* fetch data db here , got "list" */     adapter = new eventadapter(list);     listview.setadapter(adapter);     loads++;     if (adapter.getcount() == 0) {         notiftext.settext(r.string.noevents);         notiftext.setvisibility(view.visible);     } else {         notiftext.setvisibility(view.gone);     } }  private void refresh() {     if (!isadded()) return;     genericmanager.getinstance().fetchevents(mcontext, new onfinished() {         @override         public void onfinished(boolean result) {             if (result) {                 if (isadded() && getactivity() != null) {                     fetchfromdatabase();                     adapter.notifydatasetchanged();                     listview.invalidate();                     mswiperefreshlayout.setrefreshing(false);                 }             }         }     }); }  @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     view = inflater.inflate(r.layout.ptr_list, container, false);     this.inflater = inflater;     mswiperefreshlayout = (swiperefreshlayout) view.findviewbyid(r.id.swiperefresh);     mswiperefreshlayout.setcolorschemeresources(r.color.md_material_blue_600);      listview = (listview) view.findviewbyid(r.id.pull_refresh_list);     listview.setonitemclicklistener(monitemclicklistener);     listview.setonscrolllistener(new abslistview.onscrolllistener() {         @override         public void onscrollstatechanged(abslistview view, int scrollstate) {          }          @override         public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {             boolean enable = false;             if (listview != null && listview.getchildcount() > 0) {                 boolean firstitemvisible = (listview.getfirstvisibleposition() == 0);                 boolean topoffirstitemvisible = (listview.getchildat(0).gettop() == 0);                 enable = firstitemvisible && topoffirstitemvisible;             }             mswiperefreshlayout.setenabled(enable);         }     });      mswiperefreshlayout.setonrefreshlistener(new swiperefreshlayout.onrefreshlistener() {         @override         public void onrefresh() {             refresh();         }     });      progress = (imageview) view.findviewbyid(r.id.progress);     progress.setvisibility(view.gone);      notiftext = (textview) view.findviewbyid(r.id.notiftext);     notiftext.setontouchlistener(new view.ontouchlistener() {         @override         public boolean ontouch(view v, motionevent event) {              int action = event.getactionmasked();             float x = event.getx();             float y = event.gety();              switch (action) {                 case motionevent.action_down:                     break;                 case motionevent.action_move:                     float dx = x - mpreviousx;                     float dy = y - mpreviousy;                      if (dy > 300) {                         mswiperefreshlayout.post(new runnable() {                             @override                             public void run() {                                 mswiperefreshlayout.setrefreshing(true);                                 dorefresh(fromrest);                             }                         });                     }                     break;                 case motionevent.action_up:                     break;             }             mpreviousx = x;             mpreviousy = y;             return true;         }     });      settopbartitle();     return view; }  @override public void onactivitycreated(bundle savedinstancestate) {     super.onactivitycreated(savedinstancestate);     dorefresh(true); }  @override public void settopbartitle() {     topbar = (topbar) getactivity().findviewbyid(r.id.topbar);     topbartitle = r.string.events;     ((myactivity) getactivity()).settabbarvisibility(false);     topbar.setvisibility(view.visible);     super.settopbartitle(); }  @override public void onresume() {     dorefresh(false);     super.onresume(); }  /* adapter list think not affect */ 

and in myfragment have override ondettach:

  @override   public void ondetach() {     super.ondetach();     try {         field childfragmentmanager = fragment.class.getdeclaredfield("mchildfragmentmanager");         childfragmentmanager.setaccessible(true);         childfragmentmanager.set(this, null);      } catch (nosuchfieldexception e) {         throw new runtimeexception(e);     } catch (illegalaccessexception e) {         throw new runtimeexception(e);     } } 

layout of fragment b:

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.swiperefreshlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/swiperefresh"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".myactivity">      <relativelayout         style="@style/viewstyle">          <com.myapp.viewcomp.topbar             android:id="@+id/topbar"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:visibility="gone" />          <imageview             android:id="@+id/progress"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_centerinparent="true"             android:src="@drawable/progress" />          <listview             android:id="@+id/pull_refresh_list"             style="@style/ptr_style"             android:background="@color/white"             android:divider="@color/white"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:layout_below="@id/topbar" />          <textview             android:id="@+id/notiftext"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:layout_below="@id/progress"             android:layout_centerinparent="true"             android:gravity="center"             android:paddingleft="20dp"             android:paddingright="20dp"             android:visibility="gone" />     </relativelayout>  </android.support.v4.widget.swiperefreshlayout> 

i dont know why the fragment b went ondetach still freezed on screen, can see little bit of fragment @ bottom. suggestion really appreciated, stucked @ thing few days :(

i think problem in implementation of b fragment cant figure out, not sure problem swiperefreshlayout or not ...

i solved problem myself with

mswiperefreshlayout.destroydrawingcache(); 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -