Search code examples
androidandroid-loadermanager

Loader and LoaderManager - how to determine if there was an error loading


I think I understand how loaders are supposed to work, etc, but I'm not quite sure how a Loader would signal to the LoaderManager, or to the application, that it did not complete successfully. What is the best way to do this?


Solution

  • There is no built-in way. Catch all exceptions, and return some class that has an optional Exception error field. Check if the exception is null to see if the call succeeded. Something like:

    public class LoaderResult<T> {
    
        private T data;
        private Exception error;
    
        private LoaderResult(T data, Exception error) {
            this.data = data;
            this.error = error;
        }
    }