Robospice Android - spiceRequest that begins in one activity and ends in another activity -
i'm trying start robospice request in activity , receive results of request in activity b.
in activity a
fetchspicerequest fetchspicerequest = new fetchspicerequest(); spicemanager.execute(fetchspicerequest, new activityb().postlistener);
the postlistener implements pendingrequestlistener , sitting within activity b.
in activity b
we addlistener pending request started in activity below.
@override protected void onstart() { spicemanager.start( ); spicemanager.addlistenerifpending(post.class, 0, new postlistener());
we implement postlistener in activity b below.
public final class postlistener implements pendingrequestlistener<post> { @override public void onrequestfailure(spiceexception spiceexception) { toast.maketext(mainactivity.this, "exception: " + spiceexception.tostring(), toast.length_short).show(); } @override public void onrequestsuccess(post post) { toast.maketext(mainactivity.this, "added", toast.length_short).show(); } @override public void onrequestnotfound() { toast.maketext(mainactivity.this, "request not found", toast.length_short).show(); } }
finally, create variable within activity b called can pick (eg. when call new activityb().postlistener):
public postlistener postlistener;
this doesn't work , calls onrequestnotfound in postlistener.
what doing wrong?
i have looked @ post: robospice - keep spice service continue running when changing activity @snicolas doesn't seem mention how spicemanager.execute statement should in first activity.
eventually, had use localbroadcastmanager achieve want do:
activity a
fetchspicerequest fetchspicerequest = new fetchspicerequest(); spicemanager.execute(fetchspicerequest, postlistener);
please note postlistener situated within activity a. doesn't work performed inside localbroadcastreceiver:
public final class addpostlistener implements requestlistener<post> { @override public void onrequestfailure(spiceexception spiceexception) { toast.maketext(addpostactivity.this, "exception: " + spiceexception.tostring(), toast.length_short).show(); } @override public void onrequestsuccess(post post) { toast.maketext(addpostactivity.this, "added", toast.length_short).show(); log.e("addpostlistener", "background service finished"); }
inside fetchspicerequest, perform work asynchronously , once finished, call localbroadcast manager send our results activity b:
public class fetchspicerequest extends springandroidspicerequest<post> { private post mpost; private context mcontext; public addpostspicerequest(context context, post post) { super(post.class); mpost = post; mcontext = context; } @override public post loaddatafromnetwork() throws exception { //do network activity post , after network activity ends, send post through broadcast activity activity b intent intent = new intent("my-event"); intent.putextra("post", post); localbroadcastmanager.getinstance(mcontext).sendbroadcast(intent); }
activity b
register localbroadcast receiver:
@override public void onresume() { super.onresume(); localbroadcastmanager.getinstance(this).registerreceiver(mmessagereceiver, new intentfilter("my-event")); }
do received message:
private broadcastreceiver mmessagereceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { // data included in intent post post = intent.getparcelableextra("post"); log.e("receiver", "got message: " + post.getpostheader()); } };
Comments
Post a Comment