Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?
我来自iOS,它很简单,您只需使用UIViewController。 但是,在Android中,事情似乎要复杂得多,对于特定的API级别,使用某些UIComponent。 我正在阅读适用于Android的BigNerdRanch(本书大约有2年历史了),他们建议我使用
因此,对于API级别22(对API级别15或16的最低支持),我到底应该使用什么来托管组件以及组件本身? 所有这些都有用途吗?或者我应该几乎只使用一两个?
I thought Activity was deprecated
没有。
So for API Level 22 (with a minimum support for API Level 15 or 16), what exactly should I use both to host the components, and for the components themselves? Are there uses for all of these, or should I be using one or two almost exclusively?
因此,鉴于您的
-
如果要向后移植材质设计外观,请使用
AppCompatActivity -
如果不是,但是要嵌套片段,请使用
FragmentActivity -
如果不是,请使用
Activity
只是在注释中添加注释:
" <-"在这里表示继承。说不推荐使用
因此,基本上,使用
-
Activity 是基本的。 -
基于
Activity ,FragmentActivity 提供了使用Fragment 的能力。 -
AppCompatActivity 基于FragmentActivity ,为ActionBar 提供功能。
2019:使用
在撰写本文时(查看链接以确认它仍然为真),如果您正在使用应用栏,则Android文档建议使用
这是合理的:
Beginning with Android 3.0 (API level 11), all activities that use
the default theme have an ActionBar as an app bar. However, app bar
features have gradually been added to the native ActionBar over
various Android releases. As a result, the native ActionBar behaves
differently depending on what version of the Android system a device
may be using. By contrast, the most recent features are added to the
support library's version of Toolbar, and they are available on any
device that can use the support library.For this reason, you should use the support library's Toolbar class to
implement your activities' app bars. Using the support library's
toolbar helps ensure that your app will have consistent behavior
across the widest range of devices. For example, the Toolbar widget
provides a material design experience on devices running Android 2.1
(API level 7) or later, but the native action bar doesn't support
material design unless the device is running Android 5.0 (API level
21) or later.
添加工具栏的一般说明是
有关更多详细信息,请参见文档说明。他们很清楚而且很有帮助。
对于最低API级别15,您需要使用
1 2 3 4 | public class MainActivity extends AppCompatActivity { .... .... } |
要使用
1 | compile 'com.android.support:appcompat-v7:22:2.0' |
您可以将此
BigNerdRanch书是很好的资源,但是是的,它已经过时了。阅读它可获得有关Android工作原理的一般信息,但不要指望它们使用的特定类是最新的。
这里有很多混乱,特别是如果您阅读过时的资料。
基本的是
但是,还有一个支持库,其中包含您提到的其他类:
最新的一个是
如果您谈论
我们需要谈论它们正在扩展的基类,首先,我们必须了解超类的层次结构。
所有事情都是从上下文开始的,它是所有这些类的超类。
Context is an abstract class whose implementation is provided by the
Android system. It allows access to application-specific resources and
classes, as well as up-calls for application-level operations such as
launching activities, broadcasting and receiving intents, etc
The ContextWrapper is a class which extend Context class that simply
delegates all of its calls to another Context. Can be subclassed to
modify behavior without changing the original Context.
现在我们到达
The Activity is a class which extends ContextThemeWrapper that is a
single, focused thing that the user can do. Almost all activities
interact with the user, so the Activity class takes care of creating a
window for you
以下类被限制为扩展,但它们由其内部的下级扩展,并为特定的Api提供支持
The SupportActivity is a class which extends Activity that is a Base class for composing together compatibility functionality
The BaseFragmentActivityApi14 is a class which extends SupportActivity
that is a Base class It is restricted class but it is extend by
BaseFragmentActivityApi16 to support the functionality of V14The
BaseFragmentActivityApi16 is a class which extends
BaseFragmentActivityApi14 that is a Base class for {@code
FragmentActivity} to be able to use v16 APIs. But it is also
restricted class but it is extend by FragmentActivity to support the
functionality of V16.
现在为FragmentActivty
The FragmentActivity is a class which extends
BaseFragmentActivityApi16 and that wants to use the support-based
Fragment and Loader APIs.
当使用此类而不是新平台的内置片段和加载程序支持时,必须分别使用
ActionBarActivity is part of the Support Library. Support libraries are used to deliver newer features on older platforms. For
example the ActionBar was introduced in API 11 and is part of the
Activity by default (depending on the theme actually). In contrast
there is no ActionBar on the older platforms. So the support
library adds a child class of Activity (ActionBarActivity) that
provides the ActionBar's functionality and ui
2015年,支持库的版本22.1.0中弃用了ActionBarActivity。应该改用AppCompatActivity。
The AppcompactActivity is a class which extends
FragmentActivity that is Base class for activities that use the support library action bar features.
您可以在API级别7或更高级别上运行时将ActionBar添加到活动中,方法是扩展活动的此类并将活动主题设置为
我指的是这两个
由于名称可能会在将来的Android版本中更改(当前的最新名称是
AppCompatActivity扩展FragmentActivity扩展BaseFragmentActivityApi16扩展BaseFragmentActivityApi14扩展SupportActivity扩展Activity
因此,Activity比所有功能都快,而AppCompatActivity是最好的。