java - How to pass the name of the fragment to an Asynctask class? -
i have class works fine, because put name of fragment need receive information back... class call more 1 fragment need pass name parameter don't know how pass , how receive it.
this async class
public class asyncfragment extends asynctask<string, void, string> { subirfotos container; public asyncfragment(subirfotos f) { this.container = f; } @override protected string doinbackground(string... params) { try { thread.sleep(3000); // takes 3 seconds }catch(exception ex) {} return "dni activo "+params[0]+ " " + params[1]; } @override protected void onpreexecute() { super.onpreexecute(); container.showprogressbar(); } @override protected void onpostexecute(string result) { super.onpostexecute(result); // activity can null if thrown out android while task running! if(container!=null && container.getactivity()!=null) { container.populateresult(result); container.hideprogressbar(); this.container = null; } } }
this fragment
public class subirfotos extends fragment { private string userdni; private textview textview; view view; string mresult; asyncfragment mtask; public subirfotos() {} //el fragment ha sido creado @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view= inflater.inflate(r.layout.fragment_subirfotos, container, false); // on configuration changes (screen rotation) want fragment member variables preserved setretaininstance(true); return view; } //la vista de layout ha sido creada y ya está disponible @override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); } //la vista ha sido creada y cualquier configuración guardada está cargada @override public void onviewstaterestored(bundle savedinstancestate) { super.onviewstaterestored(savedinstancestate); } //el activity que contiene el fragment ha terminado su creación @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); bundle bundle = getarguments(); userdni = bundle.getstring("userdni"); textview =(textview)getview().findviewbyid(r.id.textview); ejecutarasync(userdni); if(istaskrunning(mtask)) { showprogressbar(); }else { hideprogressbar(); } if(mresult!=null) { populateresult(mresult); } super.onactivitycreated(savedinstancestate); } //el fragment ha sido quitado de su activity y ya no está disponible @override public void ondetach() { super.ondetach(); } protected void ejecutarasync(string dato) { mtask = new asyncfragment(this); mtask.execute(dato ); } public void showprogressbar() { textview resultview = (textview)getactivity().findviewbyid(r.id.texturlcontent); resultview.setvisibility(view.gone); progressbar progress = (progressbar)getactivity().findviewbyid(r.id.progressbarfetch); progress.setvisibility(view.visible); progress.setindeterminate(true); } public void hideprogressbar() { textview resultview = (textview)getactivity().findviewbyid(r.id.texturlcontent); resultview.setvisibility(view.visible); progressbar progress = (progressbar)getactivity().findviewbyid(r.id.progressbarfetch); progress.setvisibility(view.gone); } public void populateresult(string s) { textview resultview = (textview)getactivity().findviewbyid(r.id.texturlcontent); resultview.settext(s); } protected boolean istaskrunning(asyncfragment task) { if(task==null ) { return false; } else if(task.getstatus() == asyncfragment.status.finished){ return false; } else { return true; } } }
don't pass fragment name or reference. this:
create interface clients of
asynctask
subclass. interface can insideasynctask
subclass.interface onresultslistener { public void onresults(string results); }
the class should have reference listener.
private onresultslistener mlistener;
set reference in constructor of class.
public myasynctask(onresultslistener listener) { mlistener = listener; }
call listener's method on completion:
@override protected void onpostexecute(string result) { if (mlistener != null) { mlistener.onresults(result); } }
that "inversion of control" or "dependency injection" thing? did it.
your client create listener , start task
onresultslistener listener = new myasynctask.onresultslistener() { @override public void onresults(string results) { // here stuff want results } }; new myasynctask(listener).execute();
now each fragment can use same class , different things results.
Comments
Post a Comment