Fragments deprecated in Android P

…衆ロ難τιáo~ 提交于 2019-11-27 01:27:49

问题


I was looking at the documentation and found this

This class was deprecated in API level P.

Why are fragments deprecated in android P?


回答1:


The rewrite in Support Library 27.1.0

Ian's medium post (Feb 28, 2018) gives us an explanation about it. He is Android Framework Developer at Google.

Loaders in Support Library 27.1.0

For Support Library 27.1.0, I rewrote the internals of LoaderManager, the class powering the Loaders API and I wanted to explain the reasoning behind the changes and what to expect going forward.

Loaders and Fragments, a history
From the beginning, Loaders and Fragments were pretty tightly tied together at the hip. This meant that a lot of the code in FragmentActivity and Fragment were there to support Loaders, despite the fact that there are indeed fairly independent. …

What’s changed in 27.1.0
With 27.1.0, the technical debt of Loaders has been greatly reduced: …

Note: Obviously, these changes only apply to Support Library Loaders. If you are using Android framework Loaders, please switch to the Support Library Loaders as soon as possible. There are no bug fixes or improvements planned for the framework Loader APIs.

It seems like the code in Fragment and FragmentActivity has been refactored in order to make Loaders an optional dependency.

According to the release note, the new implementation is based on Lifecycle.

Important Changes
The underlying implementation of Loaders has been rewritten to use Lifecycle.

Architecture Components

In Support Library 26.1.0, Fragment and FragmentActivity has adopted Lifecycle.

This is a special release to integrate the Support Library with Lifecycles from Architecture Components. If you are not using the Lifecycles library, you don’t need to update from 26.0.2. For more information, see the Architecture Components release notes.

Important changes

  • Fragment and FragmentActivity (the base class for AppCompatActivity) now implement the LifecycleOwner interface from Architecture Components.

By contrast, Fragment and Activity in Android P have not implemented the interface LifecycleOwner.

In the Google+ post (mentioned in ThanosFisherman’s answer), Ian made a comment:

you can't change framework code after it has shipped - it is literally frozen in time. That means no new features and more importantly no bug fixes. That's not a good developer experience, particularly when we do have a fully supported, up to date, backward compatible version in the Support Library.

I think that’s the reason why Android P doesn't adopt Lifecycle. Consequently Fragment is deprecated in Android P.




回答2:


Support Library Fragments are here to stay. Google encourages you to use the Support Library versions to get consistent behavior across all API levels, backported bug fixes, and Lifecycle and ViewModel support.

Old Reference Link(Dead)

New Reference Link




回答3:


In case anyone was looking for the way to instantiate fragments by the class name.

Old way:
Fragment.instantiate(context, fragmentClass)

New way:

val fm: FragmentManager = ...
fm.fragmentFactory.instantiate(ClassLoader.getSystemClassLoader(), fragmentClass)



回答4:


Use supportFragmentManager in stead of fragmentManager in Android x.



来源:https://stackoverflow.com/questions/49446411/fragments-deprecated-in-android-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!