Activity lifecycle observer - how to know when an activity instance is destroyed?
<p>Imagine I have a class <em>MyLibrary</em>, that requires to be passed an Activity as a parameter to it's constructor: <code>public MyLibrary(Activity someActivity)</code>. Is there a way to attach an observer to <em>someActivity</em> within the <em>MyLibrary</em> constructor, so that <em>MyLibrary</em> would know when <em>someActivity</em> gets destroyed?</p>

<p>I'm using <a href="https://github.com/bumptech/glide" rel="nofollow noreferrer">Bumptech's Glide</a> for async image loading and in several places in their issues it is mentioned that after passing a context, activity or fragment to it's <code>Glide.with()</code> function, Glide will automatically pause images that are currently loading if the passed object gets destroyed. (<a href="https://github.com/bumptech/glide/issues/1436#issuecomment-243593368" rel="nofollow noreferrer">Example</a>) I'm wondering how Glide figures out when to stop loading once the given context, activity or fragment is destroyed, how does it observe it?</p>

<p>Ideally I'd like to do something along the following lines (note this is a made up example):</p>

<pre><code>public MyLibrary(Activity someActivity){
    someActivity.addLifecycleObserver(new LifecycleObserver(){
        @Override
        public void onStart() {}

        @Override
        public void onStop() {}

        @Override
        public void onDestroy() {}
    });
}
</code></pre>