I'm looking for a technical answer to how the android robospice library manages activity lifecycle. From the getting started page:
https://github.com/octo-online/robospice/wiki/Starter-Guide
"As an inner class of your Activity (or other context), add a RequestlListener that will update your UI. Don't worry about memory leaks, RoboSpice manages your activity's life cycle."
My question is how does robospice automatically update the request listeners so that it still is able to call the correct listener with the correct context after a rotation and after the activity has been destroyed and recreated as a new instance?
I've been trying to reverse engineer the source code but haven't found an answer yet:
https://github.com/octo-online/robospice
@Take Chances Make Cha. What you are saying is just completely right. RS has been designed with this express need in mind : managing network requests and activities' life cycles.
@craigrs84. 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.
The main purpose of RS is to make sure that there is no memory leak : your activity, if it has to die, will die and be garbage collected, RS doesn't hold any hard reference to it that would prevent garbage collection. That's really the core idea behind RoboSpice.
If you want a new instance of your activity to be replugged to a pending request (for instance you execute a request, then rotate the device and then get a new instance of your activity, and want that new instance to receive the result of the request executed by the previous instance), that's possible with RS.
In such a case, use the method spiceManager.addListenerIfPending
in on start, right after the call to spiceManager.start(..). This will not execute a new request, but re-plug a new listener to a pending request. If no request is pending, then it will do nothing.
The short answer, from my experience, is that it doesn't.
For example, if you don't call SpiceManager.shouldStop()
and execute a request, the reference to the RequestListener is still retained and you may end up with a memory leak as it tries to update whatever is referenced within it if your Activity/Fragment/Service no longer exists.