What is the appropriate replacement of deprecated getSupportLoaderManager()?
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:
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:
For a more thorough explanation, you can read this blog post:
Related videos on Youtube
fabi
Updated on July 09, 2022Comments
-
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 almost 6 yearsNot only this call but Loaders in general are deprecated in API 28. The suggested replacement is
ViewModel
andLiveData
. -
Fran Marzoa over 5 yearsYou 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 about 4 yearssometime on some device throw "object returned from oncreateloader must not be null"
-
-
Kavin Prabhu almost 6 yearsSorry, I just gave a wrong link. Please check the same again! I have updated the link.
-
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 almost 6 yearshow 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 over 5 yearsThis seems to remove the deprecation warning but is this the correct way?
-
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 over 5 yearsIf 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 over 5 yearsFramework 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 anActivity
orFragment
instance, because you now need to use a static factory method instead. -
ban-geoengineering about 5 yearsSo, in my
AppCompatActivity
(thatimplements LoaderManager.LoaderCallbacks<Cursor>
), all I need to do is replacegetSupportLoaderManager()
withLoaderManager.getInstance(this)
and I can leave myonCreateLoader()
,onLoadFinished()
andonLoaderReset()
methods untouched? -
BladeCoder about 5 yearsYes. You can also safely perform FragmentTransactions in onLoadFinished() now.
-
Ram about 4 yearssometime on some device throw "object returned from oncreateloader must not be null"
-
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.