android - Why is my doInBackground called so many times? -
i have navigation drawer , onclick follows:
private class slidemenuclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, final int position, long id) { // system.out.println("clicked"); mdrawerlayout.postdelayed(new runnable() { @override public void run() { displayview(position); } }, 200); } }
my displayview(position); method is:
private void displayview(int position) { // update main content replacing fragments fragment fragment = null; switch (position) { case 0: fragment = new fragmenttab1(); break; case 1: fragment = new fragmenttab2(); break; case 2: fragment = new fragmenttab3(); break; case 3: fragment = new fragmenttab3(); //create dialog break; default: break; } if (fragment != null) { 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(gravity.left); } else { // error in creating fragment log.e("mainactivity", "error in creating fragment"); } }
now in oncreateview
of fragment class call:
new listloader().execute(getactivity());
here implementation:
public class listloader extends asynctask<context, void, curadapter> { progressdialog asycdialog = new progressdialog(getactivity()); @override protected void onpreexecute() { // todo auto-generated method stub asycdialog.setmessage("working"); asycdialog.getwindow().setgravity(gravity.center_vertical); asycdialog.getwindow().setgravity(gravity.center_horizontal); asycdialog.setcancelable(false); asycdialog.show(); super.onpreexecute(); } protected void onpostexecute(curadapter result) { asycdialog.cancel(); lv.setadapter(result); // dbhelper.close(); } @override protected curadapter doinbackground(context... params) { system.out.println("loading list"); dbhelper.open(); final string status = fetch(); cursor mcur; mcur = dbhelper.magicquery(status); cur = new curadapter(getactivity(), mcur,0); cur.setfilterqueryprovider(new filterqueryprovider() { public cursor runquery(charsequence constraint) { return dbhelper.getdirectorylist(constraint,status); } }); return cur; } }
the message loading list appears each time open , close navigation drawer, making execute? have put system.out message in oncreateview
not show up, while calling asynctask oncreateview
. miss here?
i face flicker onclick of navdrawer item fragment transition, though using asynchronous method. have to, against wish use loading progress dialog.
edit:
got culprit:
searchview.onquerytextlistener textchangelistener = new searchview.onquerytextlistener() { @override public boolean onquerytextchange(string newtext){ if (textutils.isempty(newtext)){ new listloader().execute(getactivity()); } else { cursoradapter filteradapter = (cursoradapter) cur; filteradapter.getfilter().filter(newtext.tostring()); } return true; } @override public boolean onquerytextsubmit(string query) { return true; } };
edit fiddled around onquerytextlistener , resolved.
int ix =1; searchview.onquerytextlistener textchangelistener = new searchview.onquerytextlistener() { @override public boolean onquerytextchange(string newtext){ if (textutils.isempty(newtext)){ if(ix == 2) new listloader().execute(getactivity()); } else { ix = 2; cursoradapter filteradapter = (cursoradapter) cur; filteradapter.getfilter().filter(newtext.tostring()); } return true; } @override public boolean onquerytextsubmit(string query) { return true; } };
the reason:
this observed behavior when using fragment
s navigationdrawer
. each time navigationdrawer
opened, current fragment
's onpause()
, ondestroy()
called, , each time drawer closed, current fragment
's oncreateview()
, onresume()
called.
in nutshell, fragment
destroyed , created anew every open/close cycle of drawer. that's why asynctask
called repeatedly.
the solution(s):
you have 2 options:
1. save state of each fragment
in onsavedinstancestate()
, restore saved data in oncreate()
. execute asynctask
if savedinstancestate
null in oncreate()
or oncreateview()
.
2. try using activity
s instead of fragment
s. activity
s not created anew when drawer opened/closed.
Comments
Post a Comment