Android P中Fragments已被弃用:为何该组件在API Level P中被弃用?
android.app.Fragment class deprecated in Android P? Great question! Let me break down exactly why the old framework Fragment class got marked as deprecated starting in Android P (API level 28):
The shift to AndroidX (Jetpack) libraries
Google wanted to decouple fragment functionality from the core Android OS. The oldandroid.app.Fragmentwas tied directly to system updates—so bug fixes, new features, or performance improvements only rolled out when users got an OS update. The AndroidX replacement (androidx.fragment.app.Fragment) is a standalone library you can update via Gradle, meaning you can get fixes and new tools immediately, no waiting for system-level updates.Superior backward compatibility
With AndroidX Fragments, you can use modern features likeFragmentResultListener,setMaxLifecycle, or enhanced state restoration even on older Android versions (often down to API 16). The old framework fragments couldn't offer this level of cross-version support, leaving apps stuck with outdated behavior on millions of older devices.Rebuilt for better reliability & maintenance
The AndroidX Fragment library was completely redesigned with cleaner architecture, better handling of configuration changes (like screen rotations), and fewer of the edge-case bugs that plagued the old implementation. Google has stopped active development onandroid.app.Fragment—all future improvements and bug fixes are exclusively for the AndroidX version.Seamless integration with modern Android tools
As Google pushed Jetpack components (ViewModel, LiveData, Navigation Component, etc.) as the standard for building robust Android apps, the old framework fragments didn't play nicely with these tools. AndroidX Fragments are built to integrate seamlessly with the rest of Jetpack, making it easier to follow modern architecture patterns and build maintainable apps.
If you're still using the old framework fragments, the recommended path is to migrate to the AndroidX Fragment library—it's the only version that will receive ongoing support and new features.
内容的提问来源于stack exchange,提问作者RoyalGriffin

