androidandroid-asynctaskasynctaskloader

Does `LoaderManager.restartLoader` create a new object?


Does the restartLoader(int id, Bundle args, LoaderCallbacks<D> callback) create an entirely new loader object? Or does it just reuse the old one by resetting its internal state?

Thanks for any help.


Solution

  • If you pass the same ID each time you call

     restartLoader(int id, Bundle args, LoaderCallbacks<D> callback)
    

    it will REUSE the same loader object. But if you pass a new ID it will create a new Loader Object. The way this works as from the documentation check this quote from the documentation:

    Starts a new or restarts an existing Loader in this manager, registers the callbacks to it, and (if the activity/fragment is currently started) starts loading it. If a loader with the same id has previously been started it will automatically be destroyed when the new loader completes its work. The callback will be delivered before the old loader is destroyed.

    But you do not have to dig deep on this because the ID is what matters as from this Documentation guide, check the Loader Summary Here:

    To start loading data from a loader, call either initLoader() or restartLoader(). The system automatically determines whether a loader with the same integer ID already exists and will either create a new loader or reuse an existing loader.

    So in short if you pass a different ID it will create a new loader but if you pass the same ID it will use the same loader. But remember Cursors are not reused.