IllegalStateException: Cannot unregister not yet registered manager
Glide version 4.13.1
Issue: App crash in RequestManager class. IllegalStateException: Cannot unregister not yet registered manager
On checking the code in RequestManager, i see in the constructor we are adding 'this' (RequestManager) as listener to the lifecycle object (ActivityFragmentLifecycle) using fun addListener. Now in addListener fun of ActivityFragmentLifecycle, we are calling onDestroy() on this listener if lifecycle is destroyed which further calls glide.unregisterRequestManager() but as we are registering request manager only after adding the listener in RequestManager constructor, it's crashing.
This is kind of broken as expected.
The best we could do is improve the assertion so that it fails with a more obvious reason. We already try to assert that you do not call Glide.with on a destroyed activity: https://github.com/bumptech/glide/blob/master/library/src/main/java/com/bumptech/glide/manager/RequestManagerRetriever.java#L381-L385
We already have a check of activity.isDestroyed before calling Glide.with but still it seems like when multiple requests are submitted, till the time code execution calls RequestManager constructor, activity gets destroyed.
Can't we simply move the line glide.registerRequestManager(this) as the first thing in the RequestManager constructor?
This crash may happen when we call Glide.with on a destroyed fragment, because RequestManagerRetriever.get(fragment) only assert fragment.getContext() can not be null.
Preconditions.checkNotNull( fragment.getContext(), "You cannot start a load on a fragment before it is attached or after it is destroyed");
@sjudd @glide-copybara-robot To avoid this crash, can we not simply move the line glide.registerRequestManager(this) as the first thing in the RequestManager constructor? Issue is: RequestManager().constructor[line 139] > ActivityFragmentLifecycle.addListener(this)[Line 35] > RequestManager.onDestroy()[Line 378] > glide.unregisterRequestManager(this) is being called even before glide.registerRequestManager(this) is called at RequestManager().constructor[line 147]