android - Android P 中不推荐使用的 fragment

标签 android android-fragments android-9.0-pie

我在看 documentation并找到了这个

This class was deprecated in API level P.

为什么在 android P 中不推荐使用 Fragment?

最佳答案

支持库 27.1.0 中的重写

Ian's medium post (2018 年 2 月 28 日)给我们一个解释。他是 Google 的 Android 框架开发人员。

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.

FragmentFragmentActivity 中的代码似乎已被重构,以使 Loaders 成为可选依赖项。

根据the release note ,新的实现基于Lifecycle

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

架构组件

Support Library 26.1.0FragmentFragmentActivity 采用了 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.

相比之下,FragmentActivity Android P 中没有实现接口(interface)LifecycleOwner

the Google+ post (在 ThanosFisherman’s answer 中提到),Ian 发表评论:

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.

我认为这就是 Android P 不采用 Lifecycle 的原因。因此 Fragment 在 Android P 中已被弃用。

关于android - Android P 中不推荐使用的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49446411/

相关文章:

android - Gradle任务仅执行uiTests

java - 调用另一个 fragment 时 ListAdapter 错误

java - 在 android 派中重新启动应用程序时,sqlite 中输入的数据会自动删除

android - FragmentManager findFragmentById 返回 null

android - 将复杂对象从 Activity 传递到 fragment 对话框,反之亦然

java - 在没有时间源的情况下,Android源码的哪一部分决定了Android的初始时间?

java - WifiManager 中的 startScan() 在 Android P 中已弃用

java - 如何在 Java 中乘以 2 个或更多 BigDecimals?

android - Retrofit 2 如何处理有时无效有时非无效的服务器响应

android - 为版本 : Proguard config 生成签名的 apk 时出错