code: public void addListener(LifecycleListener listener)
    {
      synchronized (this) {
        if (isDestroyed()) {
          IllegalStateException e = new IllegalStateException("attempted to add listener to a destroyed lifecyle " + this);
  
          if (_log != null)
            _log.log(Level.WARNING, e.toString(), e);
          else
            Logger.getLogger(Lifecycle.class.getName()).log(Level.WARNING, e.toString(), e);
  
          return;
        }
  
        if (_listeners == null)
          _listeners = new ArrayList<WeakReference<LifecycleListener>>();
  
        for (int i = _listeners.size() - 1; i >= 0; i--) {
          LifecycleListener oldListener = _listeners.get(i).get();
  
          if (listener == oldListener)
            return;
          else if (oldListener == null)
            _listeners.remove(i);
        }
  
        _listeners.add(new WeakReference<LifecycleListener>(listener));
      }
    }
docs: Adds a listener to detect lifecycle changes.