Android Activity 和 FragmentActivity 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10477997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:54:06  来源:igfitidea点击:

Difference between Activity and FragmentActivity

androidandroid-fragmentsandroid-support-libraryandroid-fragmentactivity

提问by Rookie

I was working on fragments and came across two things Activityand FragmentActivitywhich are used several times. I want to know that is there any difference between these two, because when I changed Activitywith FragmentActivity, it had no effect on the app.

我在处理片段时遇到了两件事ActivityFragmentActivity并且被多次使用。我想知道这两者之间有什么区别,因为当我更改Activity为 时FragmentActivity,它对应用程序没有影响。

回答by Alex Lockwood

A FragmentActivityis a subclass of Activitythat was built for the Android Support Package.

AFragmentActivityActivityAndroid Support Package构建的子类。

The FragmentActivityclass adds a couple new methods to ensure compatibility with older versions of Android, but other than that, there really isn't much of a difference between the two. Just make sure you change all calls to getLoaderManager()and getFragmentManager()to getSupportLoaderManager()and getSupportFragmentManager()respectively.

FragmentActivity类增加了一些新的方法,以确保与旧版本的Android的兼容性,但除此之外,真的没有多少两者之间的差别。只要确保你改变所有来电getLoaderManager()getFragmentManager()getSupportLoaderManager()getSupportFragmentManager()分别。

回答by Samuel Peter

FragmentActivityis part of the support library, while Activityis the framework's default class. They are functionally equivalent.

FragmentActivity支持库的一部分,而Activity是框架的默认类。它们在功能上是等效的。

You should always use FragmentActivityand android.support.v4.app.Fragmentinstead of the platform default Activityand android.app.Fragmentclasses. Using the platform defaults mean that you are relying on whatever implementation of fragments is used in the device you are running on. These are often multiple years old, and contain bugs that have since been fixed in the support library.

您应该始终使用FragmentActivityandandroid.support.v4.app.Fragment而不是平台默认值Activityandroid.app.Fragment类。使用平台默认值意味着您依赖于您正在运行的设备中使用的任何片段实现。这些通常已有多年历史,并且包含已在支持库中修复的错误。