27

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?

Saurabh Kumar
  • 437
  • 1
  • 5
  • 18
fabi
  • 454
  • 1
  • 5
  • 12
  • 3
    Not only this call but Loaders in general are deprecated in API 28. The suggested replacement is `ViewModel` and `LiveData`. – Henry Jul 18 '18 at 17:49
  • 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. – Fran Marzoa Nov 17 '18 at 15:53
  • sometime on some device throw "object returned from oncreateloader must not be null" – Ram Feb 21 '20 at 06:12

6 Answers6

23

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

i hope it helps

amos godwin
  • 930
  • 9
  • 12
  • sometime on some device throw "object returned from oncreateloader must not be null" – Ram Feb 21 '20 at 06:13
20

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).

BladeCoder
  • 12,779
  • 3
  • 59
  • 51
  • 4
    So, in my `AppCompatActivity` (that `implements LoaderManager.LoaderCallbacks`), all I need to do is **replace `getSupportLoaderManager()` with `LoaderManager.getInstance(this)`** and I can leave my `onCreateLoader()`, `onLoadFinished()` and `onLoaderReset()` methods untouched? – ban-geoengineering Mar 27 '19 at 17:46
  • 1
    Yes. You can also safely perform FragmentTransactions in onLoadFinished() now. – BladeCoder Mar 27 '19 at 20:45
15

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.

Abandoned Cart
  • 4,512
  • 1
  • 34
  • 41
Kavin Prabhu
  • 2,307
  • 2
  • 17
  • 36
  • Sorry, I just gave a wrong link. Please check the same again! I have updated the link. – Kavin Prabhu Jul 18 '18 at 17:56
  • 3
    @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. – Henry Jul 19 '18 at 03:47
  • 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. – fabi Jul 19 '18 at 21:16
  • 1
    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. – BladeCoder Dec 13 '18 at 00:45
9

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

MohammadL
  • 2,398
  • 1
  • 19
  • 36
  • 2
    This seems to remove the deprecation warning but is this the correct way? – Amrut Oct 26 '18 at 06:43
  • 3
    @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. – MohammadL Oct 26 '18 at 10:46
  • 2
    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. – Fran Marzoa Nov 17 '18 at 15:50
  • @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. – Abandoned Cart Mar 09 '20 at 16:49
7

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

Fran Marzoa
  • 4,293
  • 1
  • 37
  • 53
4

Same problem occurred with me!

getSupportLoaderManger() can be replaced by LoaderManager.getInstance(this) and the further code remains the same. So, finally your code will become:

LoaderManager.getInstance(this).initLoader(0, null, mRecipeLoaderManager);

You can refer Android official documentation: https://developer.android.com/reference/androidx/fragment/app/FragmentActivity#getSupportLoaderManager()

Priyansh jain
  • 1,065
  • 10
  • 17