5

I am developing an app using RoboSpice library for calling REST API's. Everything works fine with the library except one thing. I don't use caching available in RoboSpice and so all the requests are made without cache. Now, when any request is going on and the user presses the home button then onStop() is called where shouldStop() of spice manager is called which unregisters all the request listeners for notification. When, the app again comes to foreground then UI update doesn't occur as the listeners have not been notified.

I don't want to use Cache that Robospice offers. Is there any other way that one can get UI update notifications without using Cache??

thefrugaldev
  • 1,619
  • 4
  • 18
  • 36

1 Answers1

4

It's clearly not possible with RoboSpice, and indeed, there is a good reason : it would be a very bad idea.

When your activity dies (onStop), Android wants to garbage collect its instance. And to do it, it should not be referenced by anything. That's why RS imposes that all listeners are removed. Typically listeners hold a reference to the activity (as inner classes) and the finest achievement of RS is to let the activity die properly and get garbage collected.

So, doing what you want would clearly lead to a memory leak, and moreover would lead to crashes most of the time: when an activity is not displayed anymore, you would like to update its UI ? Looks a bit ackward, doesn't it ?

Maybe the simplest would be to use a very limited cache, or just execute all your requests every time your activities enter in onStart.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • 1
    I do agree that RS is doing it for a reason. But, just imagine that a request has been made and progress dialog is shown and user presses the home button. The call to REST will go through properly and when the user starts the app the progress dialog has not gone because the UI is not updated as listeners were unregistered. Anyways, it seems that using cache is the only way with RS at present. – thefrugaldev Jan 16 '14 at 08:10
  • The other way can be calling spiceManager.shouldStop() in onDestroy and starting the spicemanager in onCreate(). As onDestroy() is the final call that an activity will get if its being killed. – thefrugaldev Jan 16 '14 at 08:19
  • @mobiledev, I don't think the second solution is any better. Service binding is really tricky and the preferred place is to use the couple onStart/onStop. You might be interested in doing the request from a service instead of an activity. – Snicolas Jan 16 '14 at 10:50
  • I think RS already makes a request using Service. Anyways, will see if I can use cache. – thefrugaldev Jan 16 '14 at 11:30
  • Sure it does, but the launching context can also be a Service, not only an activity or a fragment. You would thus have a longer living context to execute your request from and get living listeners. Service can, for instance, update a notification, showing progress. – Snicolas Jan 16 '14 at 12:04
  • @mobiledev do you know how to use cache? we have to use **getSpiceManager().getFromCache(Contributor.List.class, "github", DurationInMillis.ONE_MINUTE, new ListContributorRequestListener());** in _onResume_ ? not working for me :( [in retrofit RC](https://github.com/octo-online/RoboSpice-samples/tree/release/robospice-sample-retrofit) ,click on the notification also not firing the activity!! – LOG_TAG Apr 29 '14 at 07:53
  • @LOG_TAG what do you mean by not firing activity ? The listener methods ain't called ? – Snicolas Apr 29 '14 at 12:29
  • @snicolas click on the notification ! (Pending intent) when activity is closed before the n/w response app will get notified, click on that notification nothing happens! Then clicking on the recent apps activity opens without any data! – LOG_TAG Apr 29 '14 at 13:05
  • So, in case I post or edit something, which typically doesn't need to cache, (and then show progress indicator -> activity stopped -> request completed -> activity started), there is no way to know if it was success/completed without using cache? – fikr4n Jan 29 '17 at 05:39