Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does robospice manage activity lifecycle?

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

like image 477
craigrs84 Avatar asked Sep 25 '13 17:09

craigrs84


People also ask

How does Android system manage activity life cycle?

Activity-lifecycle concepts To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of six callbacks: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() . The system invokes each of these callbacks as an activity enters a new state.

Is ViewModel Life Cycle Aware?

Lifecycle Awareness: ViewModel objects are also lifecycle-aware. They are automatically cleared when the Lifecycle they are observing gets permanently destroyed.

When in the activity lifecycle is onSaveInstanceState () called?

Note that onSaveInstanceState() is called when your activity goes into the background and NOT when the app process is about to be killed.


2 Answers

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

like image 125
Snicolas Avatar answered Sep 18 '22 00:09

Snicolas


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.

like image 34
AllDayAmazing Avatar answered Sep 17 '22 00:09

AllDayAmazing