I am creating apps using Robospice library
. It is great choice to handle internet connections, because core of the library is based on Android Service, so our connections are not tied to the Activity life-cycle.
We are creating our requests and executing them with spice manager which in turn is instantiated in each activity (Base Activity inheritance) , I don't is it right way to place creating of manage object here, if there is better way to do this, please let me know.
public class BaseActivity extends AppCompatActivity implements ActivityController, SpiceManagerProvider {
private SpiceManager mSpiceManager = new SpiceManager(MyRobospiceService.class);
I have been creating request (robospice requests) exactly where I need them in fragments and activities. But now I thought a little bit about this. Maybe it is better to separate request handling only in activities. And just listening for button clicks or whatever from fragments in activity with callback methods or some other inter component communication. And than in activity make request, handle it. But in this case if I need to get data back in fragment I have to send it back from activity to the fragment. So it seems a lot of redundant communications.
All in all I wan't to get advice from more experienced developers about separation of responsibilities, should I handle request in only one component (like activity) or I can make and handle requests anywhere I need this.
Thanks everyone in advance.
Using a single SpiceManager
per Activity
and sharing it between Fragment
s is what I have successfully done in the past. You have to, unfortunately, check if the Activity
is still resumed in this case for every response listener.
To be specific, you need to be sure that you will not update a stopped or destroyed UI.
See related FAQ question for more details.
The second paragraph there mentions the the other approach of having a SpiceManager
per Fragment
, so that should be a viable option for you as well. We noticed some overhead while creating SpiceManager
s (this only hurts when there are many Fragment
s) and therefore abandoned it for our use.