What is the appropriate replacement of deprecated getSupportLoaderManager()?
Asked Answered
I

6

29

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?

Ibadan answered 18/7, 2018 at 17:44 Comment(3)
Not only this call but Loaders in general are deprecated in API 28. The suggested replacement is ViewModel and LiveData.Bigener
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.Portiaportico
sometime on some device throw "object returned from oncreateloader must not be null"Ellyellyn
H
17

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.

Hispanicize answered 18/7, 2018 at 17:51 Comment(4)
Sorry, I just gave a wrong link. Please check the same again! I have updated the link.Hispanicize
@Ibadan 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.Bigener
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.Ibadan
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.Swain
G
24

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

i hope it helps

Goahead answered 13/3, 2019 at 21:11 Comment(1)
sometime on some device throw "object returned from oncreateloader must not be null"Ellyellyn
S
21

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

Swain answered 13/12, 2018 at 0:38 Comment(2)
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?Piscine
Yes. You can also safely perform FragmentTransactions in onLoadFinished() now.Swain
H
17

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.

Hispanicize answered 18/7, 2018 at 17:51 Comment(4)
Sorry, I just gave a wrong link. Please check the same again! I have updated the link.Hispanicize
@Ibadan 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.Bigener
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.Ibadan
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.Swain
K
9

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

Kurtz answered 10/10, 2018 at 13:34 Comment(4)
This seems to remove the deprecation warning but is this the correct way?Totaquine
@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.Kurtz
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.Portiaportico
@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.Chuffy
P
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

Portiaportico answered 17/11, 2018 at 16:34 Comment(0)
T
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()

Theiss answered 31/5, 2020 at 7:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.