Fragments 和 DialogFragments 已弃用,什么将取代它们?

Fragments and DialogFragments deprecated, what will replace them?

我们可以在官方Android文档中阅读

android.app.Fragment
This class was deprecated in API level 28. Use the Support Library Fragment for consistent behavior across all devices and access to Lifecycle.This class was deprecated in API level 28.

这是怎么回事?
我们现在应该使用什么来代替 Fragments 和 DialogFragments?

您只需更改导入语句。

发件人:android.app.Fragment 收件人:android.support.v4.app.Fragment

就是这样

现在,您可以使用来自 Android P(28) 的片段支持库 (AndroidX)。

gradle.properties中添加以下内容

android.useAndroidX=true
android.enableJetifier=true 

在项目中添加如下依赖build.gradle

implementation 'androidx.fragment:fragment:1.0.0'

更改导入语句

片段: import android.app.Fragment;

DialogFragment: import android.app.DialogFragment;

片段: import androidx.fragment.app.Fragment;

DialogFragment: androidx.fragment.app.DialogFragment;

参考 link 由 Google 提供:https://developer.android.com/jetpack/androidx/migrate