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
Difference between Activity and FragmentActivity
提问by Rookie
I was working on fragments and came across two things Activity
and FragmentActivity
which are used several times. I want to know that is there any difference between these two, because when I changed Activity
with FragmentActivity
, it had no effect on the app.
我在处理片段时遇到了两件事Activity
,FragmentActivity
并且被多次使用。我想知道这两者之间有什么区别,因为当我更改Activity
为 时FragmentActivity
,它对应用程序没有影响。
回答by Alex Lockwood
A FragmentActivity
is a subclass of Activity
that was built for the Android Support Package.
AFragmentActivity
是Activity
为Android Support Package构建的子类。
The FragmentActivity
class 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
FragmentActivity
is part of the support library, while Activity
is the framework's default class. They are functionally equivalent.
FragmentActivity
是支持库的一部分,而Activity
是框架的默认类。它们在功能上是等效的。
You should always use FragmentActivity
and android.support.v4.app.Fragment
instead of the platform default Activity
and android.app.Fragment
classes. 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.
您应该始终使用FragmentActivity
andandroid.support.v4.app.Fragment
而不是平台默认值Activity
和android.app.Fragment
类。使用平台默认值意味着您依赖于您正在运行的设备中使用的任何片段实现。这些通常已有多年历史,并且包含已在支持库中修复的错误。