0

I'm trying to start a robospice request in Activity A and then receive the results of the request in Activity B.

In Activity A

FetchSpiceRequest fetchSpiceRequest = new FetchSpiceRequest();
spiceManager.execute(fetchSpiceRequest, new ActivityB().postListener);

The PostListener implements PendingRequestListener and is sitting within Activity B.

In Activity B

We addListener for the pending request that was started in Activity A below.

@Override
protected void onStart() {
    spiceManager.start( this );
    spiceManager.addListenerIfPending(Post.class, 0, new PostListener());

We then implement the 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, we create a variable within Activity B called so that A can pick it up (eg. when I call new ActivityB().postListener):

public PostListener PostListener;

This doesn't work and it always calls onRequestNotFound in the PostListener.

What am I doing wrong?

I have also looked at this post: Robospice - keep spice service continue running when changing activity but @Snicolas doesn't seem to mention anything about how the spiceManager.execute statement should look like in the first activity.

Community
  • 1
  • 1
Simon
  • 19,658
  • 27
  • 149
  • 217
  • Can you make completely sure that the request is not really finished during the change of activities? – nekojsi Jun 19 '15 at 07:58
  • I'm not sure what I want to achieve is possible. I was reading Snicolas response here: http://stackoverflow.com/questions/19011200/how-does-robospice-manage-activity-lifecycle?rq=1 "Basically, what happens with RS is that when a request is being processed, its listeners will be invoked as long as the associated activity is alive. If the activity is not alive anymore, all of its listeners are unplugged from RS, and they will not be notified." – Simon Jun 19 '15 at 18:14

1 Answers1

0

Eventually, I had to use LocalBroadcastManager to achieve what I want to do:

Activity A

FetchSpiceRequest fetchSpiceRequest = new FetchSpiceRequest();
spiceManager.execute(fetchSpiceRequest, postListener);

Please note that postListener is situated within Activity A. It actually doesn't do much as all the work will now be performed inside the 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 the fetchSpiceRequest, we perform the work asynchronously and once it is finished, we call the localbroadcast manager to send our results back to 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 some network activity to get a post and then after the network activity ends, send the post through the broadcast activity to Activity B
        Intent intent = new Intent("my-event");
        intent.putExtra("post", post);
        LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);   
}

Activity B

Register your localBroadcast receiver:

@Override
public void onResume() {
    super.onResume();
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
            new IntentFilter("my-event")); }

Do something with the received message:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Get extra data included in the Intent
        Post post = intent.getParcelableExtra("post");
        Log.e("receiver", "Got message: " + post.getPostheader());
    }
};
Simon
  • 19,658
  • 27
  • 149
  • 217