3

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?

mogronalol
  • 2,946
  • 8
  • 38
  • 56
  • For which purpose you are using Observer? – Dharmendra Sep 29 '12 at 14:46
  • I am modifying domain objects via ormlite. All activities which render these objects respond to Create / Read / Update modifications via the observer notification. So for example my observerable allows me to add a new domain object to the database via an OrmLite repository, and an activity viewing a list of all domains is notified of the new domain via it's observer implementation. – mogronalol Sep 29 '12 at 15:02

2 Answers2

2

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.

Tom Mulcahy
  • 4,847
  • 1
  • 13
  • 5
  • So it will terminate the entire app / process without calling onDestroy(), but when terminating individual activities it will always call onDestroy((? – mogronalol Sep 29 '12 at 23:53
  • 1
    It doesn't quite make sense to talk about an Activity being "terminated". When the OS wants to re-use some resources held by an Activity, it calls onDestroy to let your program know that that Activity is no longer valid. The Activity itself is just an instance of a class, so it remains until it's garbage collected, but most methods will throw exceptions if called. This is in contrast to a process, which really can be terminated by the OS at will. – Tom Mulcahy Oct 02 '12 at 23:13
1

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

DayS
  • 1,561
  • 11
  • 15
  • yes, I am under similar confusion as to whether it is called, looks like weak references could solve my problem then – mogronalol Sep 29 '12 at 15:42