Robospice - continue to work with spices when changing activity

I used the RoboSpice library to perform asynchronous tasks. From their examples, the spice service is declared in BaseActivity, it starts in onStart mode and stops onStop activity. Everything is fine, but when I want to download a file from the Internet, and then I move on to another action, this download task is canceled because the spice service is stopped. It looks like this:

public abstract class BaseActivity{ /** The request manager. */ private SpiceManager requestManager = new SpiceManager(RequestService.class); @Override protected void onStart() { requestManager.start(this); super.onStart(); } @Override protected void onStop() { requestManager.shouldStop(); super.onStop(); } } 

So, I am wondering if there is a safe way to continue the download task from the Spice service (this task does not affect the user interface), and other tasks work fine (they can be canceled when stopped), but this can still keep up with the life cycle of the activity.

+8
android android-service robospice
source share
2 answers

@ R4j, you missed the point. Requests do not stop when the action holding the spicemanager terminates. In RS, requests will have their own life cycle and will be displayed in a different context (spice service, which is an Android service).

Thus, if you run a request, your activity dies or is killed, the spice request will go its own way, and the download will continue. An analysis will also occur, and finally your result will be cached. If there.

So, if you want to call a query in step A and get the result in step B, you must:

  • just complete the query in step A, as usual, follow the RS patterns
  • in B, after running your spiceManager (i.e. right after super.onStart ()), do 2 things:

    • use spiceManager.addListenerIfPending to replace a new action with any pending request
    • use spiceManager.getDataFromCache to get any result that has already been cached using the previous query.
  • optionnally, if you want to re-execute the request in B itself, you can, and you will be happy to know that if you use the same request class with the same cache key, RS will aggregate your new request for any request that is waiting for you.

In RS, a request is identified by a composite key consisting of a "query result class" + a "query cache". This composite key can be used to retrieve the pending request, as well as any result set by this request in the cache.

+11
source share

Declare an instance of spicemanager in your application class and access it in action to start the background service.

 public class MyApplication extends Application { private static MyApplication instance; private SpiceManager spiceManager = new SpiceManager(RequestService.class); @Override public void onCreate() { super.onCreate(); } public SpiceManager getManager() { return spiceManager; } } 

In action, you can call. See my code below.

 ((MyApplication) getApplicationContext()).getSpiceManager().startService(........); 
+2
source share

All Articles