Question

I am developing for android using android annotations. I have a singleton observable object for which I want to add activity as an observer. Something like as follows:

@EActivity(R.layout.myActivity)
public class MyActivity extends SherlockActivity implements Observer { 

    @Bean //has singleton scope
    protected Observerable o;

    @Override
    public void onCreate() {
        o.registerObserver(this);
    }

    @Override
    public void onDestroy() {
        o.unregisterObserver(this);
    }

    //more code

My question lies with the on destroy method. Will there be a situation where android kills the activity during a stopped or paused state without calling onDestroy()? If this is the case my Observerable could be keeping a live reference to an activity that is no longer used by the system, which is not good. Or is this not the case? If it is my presumption would be to keep weak references to all my activities in my list of registered observers, so they can be unregistered automatically. Is this a good solution or is it not needed?

Was it helpful?

Solution

The OS can kill your process without calling onDestroy, but otherwise you can rely on onDestroy being called. So provided your Observable is in the same process as the activity, you're fine.

OTHER TIPS

According to the activity's lifecycle on the Android documentation, this case could happen if an other application have to start and the system doesn't have enough memory.

Edit: Well, it seems the system will still call this method in this case. I'm not really sure about the workflow anymore...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top