2

I am using Retrofit and want REST calls to survive when the user navigates between my activities (as opposed to just retaining them accross configuration changes). Couple of options:

  1. Retrofit syncronous call within an AsyncTask and put that in a retained "task fragment"
  2. Retrofit syncronous call within an IntentService and broadcast
  3. Google suggests a per-app singleton, at least with Volley...

What is the best approach? And I can see RxJava as a Retrofit mode but even there one is just adviced to "unsubscribe from the returned Observable in onDestroy at the latest" - so not a real solution, despite all hype about the new built-in Rx support.

Gabor
  • 7,352
  • 4
  • 35
  • 56
  • Hey, try looking at the source code generated by http://androidbootstrap.com it's a nice start-out in my opinion. – EpicPandaForce Oct 07 '14 at 15:29
  • seems like it generates a fragment-based UI which is not what I have, and I'm not sure what solution did you find there to my question (so please help me understand as I don't want to just copy-paste) – Gabor Oct 07 '14 at 15:33
  • what do you want to do with the response? – pomber Oct 07 '14 at 16:44
  • @pomber believe it or not I'm designing an app that communicates with a rest service – Gabor Oct 09 '14 at 08:51
  • The best approach depends on what you want to do with the response, for example if the response doesnt change anything in the UI you may use a service, if it always do the same you may use a broadcast receiver and a fragment – pomber Oct 09 '14 at 13:34

1 Answers1

2

I don't think a retained fragment will achieve what you want here. It can help with handling configuration changes, but not between activities.

You probably should break your problem into 2 problems, one is where to start the network call, and other is receiving the response.

For the first problem, you can start the network call in the Activity/Fragment normally. If you're going to do this a lot though, you'll probably want to manage the calls in a Service and use some sort of queue.

To receive the response, you should just make sure your listener (Callback or Subscriber) isn't tied to your Activity/Fragment instance (using static for nested classes, or top-level classes). You can then forward the result to the Activity using a broadcast or EventBus/Otto.

This can be either handled in you Callback/Subscriber, or inside Service context. (Though if you want to use Broadcast inside Activity, you should reference Context.getApplicationContext() instead of the current Activity context, in case your Activity is destroyed)

Doing this your network calls should resume normally without errors, even when the Activity that requested them is stopped.

The final step would be to cache the response, because if the Activity is stopped, then the response will go no where. Even though the network call was completed, it was wasted. You should cache these responses in some way so your Activity/Fragment can retrieve them upon recreation.

Personally I would go for Service with EventBus combination.

Hassan Ibraheem
  • 2,329
  • 1
  • 17
  • 23