java - Notify a Fragment of onPostExecute from MainActivity -
in android, i'm having trouble figuring out how, when asynctask complete, notify fragment listening. seems usual java stuff, propertychangelistener
not available. in onpostexecute
, want fire event , have otherfragment
see that.
how do this?
public class mainactivity extends fragmentactivity { <stuff> private class consumewebservice extends asynctask <string, // type of parameters sent task upon execution - doinbackground() integer, // type of progress units published during background computation -onprogressupdate() string> { public consumewebservice(somekindoflistener listener){ this.mylistener = listener; } protected string doinbackground(string... urls) { < json data restful service> < create , populate sqlite db wit json data> return jsondata; } @override protected void onpostexecute(string result) { try { <fire kind of event> } catch (exception e) { } } } } public class otherfragment extends fragment implements somekindoflistener { @override public void somekindofchange(somekindofchangeevent arg0) { < stuff want do> } } public interface somekindoflistener { void onpostexecutedone(); }
i this:
1 - have interface
public interface onxlistener { void onx(object data); }
2- have fragment implement it
public class myfragment extends fragment implements onxlistener{ public void onx(object data) { dosomething(data); } public void dosomething(data) { /do real thing } }
3- call in actvitiy when asynctask done
public class myactivity extends activity { fragment mmyfragment fragment; //stuff, initializate fragment, show fragment, etc. private class myasynctask extends asynctask { //stuff @override protected void onpostexecute(object result) { ((onxlistener) myactivity.this.mmyfragment).onx(result); } } }