How does robospice manage activity lifecycle?

江枫思渺然 提交于 2019-12-22 03:49:07

问题


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


回答1:


@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.




回答2:


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.



来源:https://stackoverflow.com/questions/19011200/how-does-robospice-manage-activity-lifecycle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!