问题
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?
回答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 developer site and see the class or function you're looking for and they already mark it as deprecated
and there will be a better alternative.
回答2:
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
).
回答3:
I also had this issue too and this code solved it for me LoaderManager.getInstance(this).initLoader(0,null,mRecipeLoaderManager);
i hope it helps
回答4:
You can still use getSupportLoaderManager
if you need to as:
android.support.v4.app.LoaderManager.getInstance(this).initLoader(0, null, this).forceLoad();
回答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:
https://medium.com/google-developers/lifecycle-aware-data-loading-with-android-architecture-components-f95484159de4
来源:https://stackoverflow.com/questions/51408098/what-is-the-appropriate-replacement-of-deprecated-getsupportloadermanager