哪个 Activity/Fragment/FragmentManager?

Which Activity / Fragment / FragmentManager?

我是 Android 编程的新手,我完全不知道我应该从哪个 Activity 和 Fragment 版本派生以及我应该使用哪个 FragmentManager。 我想支持 API 级别 15 及以上,我想使用 androidx/Jetpack。 那么,我的 MainActivity 应该从哪个 Activity 实现派生?我的片段应该从哪个片段实现派生?我应该使用哪个 FragmentManager 实现?

谢谢!

activity 应该派生自 AppCompatActivity

来自https://codelabs.developers.google.com/codelabs/kotlin-android-training-app-anatomy/index.html?index=..%2F..android-kotlin-fundamentals#2

AppCompatActivity is a subclass of Activity that supports all modern Android features while providing backward compatibility with older versions of Android. To make your app available to the largest number of devices and users possible, always use AppCompatActivity.

一般来说,如果有一个 androidx 包(以前是 android 支持)有 class您正在寻找的类型使用这个,因为它们具有向后兼容性。

另一种了解使用哪个 activity 的方法是使用 android 工作室菜单,如果您创建一个新的 activity(文件/新建/Activity/基本activity)可以看到生成的文件使用了androidx.appcompat.app.AppCompatActivity.

片段片段(文件/新建/片段)也是如此,然后转到源文件,您可以在导入中看到androidx.fragment.app.Fragment。用这个。

关于 FragmentManager,如果您使用 FragmentManager 搜索 androidx 包,您将得到 androidx.fragment.app.FragmentManager.

引用 https://codelabs.developers.google.com/codelabs/kotlin-android-training-images-compat/#5

Tip: In general, if your app can use a compatibility class from the Jetpack libraries, it should use one of those classes, because those classes provide support for the largest possible number of features and devices.

引用具有 androidx 命名空间的 classes。