What is the appropriate replacement of deprecated getSupportLoaderManager()?

23,777

Solution 1

As stated here: Loaders

"Loaders have been deprecated as of Android P (API 28). The recommended option for dealing with loading data while handling the Activity and Fragment lifecycles is to use a combination of ViewModels and LiveData."

Whenever you see something is deprecated, go directly to the developer api reference site and review the class or function for which you're looking and if there is an equivalent alternative.

Solution 2

I also had this issue too and this code solved it for me LoaderManager.getInstance(this).initLoader(0,null,mRecipeLoaderManager);

i hope it helps

Solution 3

The reason why this method is deprecated is because Loaders have been unbundled from their historical Fragment and FragmentActivity implementations in order to live in their own library that will soon be an optional dependency, and their implementation has been rewritten on top of architecture components.

The unbundled way of retrieving a LoaderManager instance is to use the static factory method:

LoaderManager.getInstance(T)

where T is an instance of both LifecycleOwner and ViewModelStoreOwner (the main implementations being FragmentActivity and Fragment).

Solution 4

You can still use getSupportLoaderManager if you need to as: android.support.v4.app.LoaderManager.getInstance(this).initLoader(0, null, this).forceLoad();

Solution 5

Here you have a brief explanation on how to replace Loaders with ViewModel:

https://developer.android.com/jetpack/arch/viewmodel#loaders

The graphics there are self-explanatory, I think:

enter image description here

enter image description here

For a more thorough explanation, you can read this blog post:

https://medium.com/google-developers/lifecycle-aware-data-loading-with-android-architecture-components-f95484159de4

Share:
23,777

Related videos on Youtube

fabi
Author by

fabi

Updated on July 09, 2022

Comments

  • fabi
    fabi almost 2 years

    I came to know that getSupportLoaderManager is deprecated. But I want to call:

    getSupportLoaderManager().initLoader(0, null, mRecipeLoaderManager);
    

    What should be an alternative to that call? Or can I still use getSupportLoaderManager without worrying?

    • Henry
      Henry almost 6 years
      Not only this call but Loaders in general are deprecated in API 28. The suggested replacement is ViewModel and LiveData.
    • Fran Marzoa
      Fran Marzoa over 5 years
      You should NOT continue using loaders unless there's a good reason to do so (i.e. a whole lot of legacy code that depends on using them). For new code, the right thing to do is to NOT use deprecated code, that's why deprecation exists in first place.
    • Ram
      Ram about 4 years
      sometime on some device throw "object returned from oncreateloader must not be null"
  • Kavin Prabhu
    Kavin Prabhu almost 6 years
    Sorry, I just gave a wrong link. Please check the same again! I have updated the link.
  • Henry
    Henry almost 6 years
    @fabi Just look at the big blue note at the beginning of this page. There is no 1 to 1 replacement for this call, Loaders in general are deprecated.
  • fabi
    fabi almost 6 years
    how could I miss that…allright thanks! Seams to me as a good move to deprecate Loaders and handle those cases with ViewModel and LiveData.
  • Amrut
    Amrut over 5 years
    This seems to remove the deprecation warning but is this the correct way?
  • MohammadL
    MohammadL over 5 years
    @Amrut it removes the depreciation because it uses the support library v4. It is the correct way if you want still use Loader. Otherwise, you should use a ModelView rather than a Loader.
  • Fran Marzoa
    Fran Marzoa over 5 years
    If you want to use the Loader, you don't need to do that: you just use it. And if you want to suppress the deprecation message, then correct way to do it is to use the @SuppressWarnings("deprecation") annotation just before the statement.
  • BladeCoder
    BladeCoder over 5 years
    Framework Loaders have been deprecated but the Loaders from support libraries/AndroidX are not deprecated yet to this date according to the documentation. What is deprecated is the method to retrieve a LoaderManager from an Activity or Fragment instance, because you now need to use a static factory method instead.
  • ban-geoengineering
    ban-geoengineering about 5 years
    So, in my AppCompatActivity (that implements LoaderManager.LoaderCallbacks<Cursor>), all I need to do is replace getSupportLoaderManager() with LoaderManager.getInstance(this) and I can leave my onCreateLoader(), onLoadFinished() and onLoaderReset() methods untouched?
  • BladeCoder
    BladeCoder about 5 years
    Yes. You can also safely perform FragmentTransactions in onLoadFinished() now.
  • Ram
    Ram about 4 years
    sometime on some device throw "object returned from oncreateloader must not be null"
  • Abandoned Cart
    Abandoned Cart about 4 years
    @FranMarzoa The entire premise of the support package is to maintain support across versions, especially for items that are removed later or weren't available before. Simply suppressing the warnings is the quickest way to cause issues down the road.