关于android:Activity,AppCompatActivity,FragmentActivity和ActionBarActivity:何时使用哪个?

Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?

我来自iOS,它很简单,您只需使用UIViewController。 但是,在Android中,事情似乎要复杂得多,对于特定的API级别,使用某些UIComponent。 我正在阅读适用于Android的BigNerdRanch(本书大约有2年历史了),他们建议我使用Activity托管我的FragmentActivities。 但是,我认为不推荐使用Activity

因此,对于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?

Activity是基线。每个活动都直接或间接地从Activity继承。

FragmentActivity用于在support-v4support-v13库中找到的片段的反向移植。片段的本机实现在API级别11中添加,该级别低于建议的minSdkVersion值。需要特别考虑FragmentActivity的唯一原因是,如果您要使用嵌套片段(包含另一个片段的片段),因为本机片段直到API级别17才支持。

AppCompatActivity来自appcompat-v7库。原则上,这提供了操作栏的反向移植。由于本机操作栏是在API级别11中添加的,因此您不需要AppCompatActivity。但是,就动作栏和各种小部件而言,当前的appcompat-v7版本也增加了有限的材料设计美感。使用appcompat-v7的优缺点,远远超出了此特定Stack Overflow答案的范围。

ActionBarActivityappcompat-v7中基础活动的旧名称。由于各种原因,他们想更改名称。除非您使用的某些第三方库坚持使用ActionBarActivity,否则您应该更喜欢AppCompatActivity而不是ActionBarActivity

因此,鉴于您的minSdkVersion在15-16范围内:

  • 如果要向后移植材质设计外观,请使用AppCompatActivity

  • 如果不是,但是要嵌套片段,请使用FragmentActivity

  • 如果不是,请使用Activity

只是在注释中添加注释:AppCompatActivity扩展了FragmentActivity,因此任何需要使用FragmentActivity功能的人都可以使用AppCompatActivity


Activity是所有其他活动的基类,我认为它不会被弃用。它们之间的关系是:

Activity <-FragmentActivity <-AppCompatActivity <-ActionBarActivity

" <-"在这里表示继承。说不推荐使用ActionBarActivity的引用,请改用AppCompatActivity

因此,基本上,使用AppCompatActivity始终是正确的选择。它们之间的区别是:

  • Activity是基本的。
  • 基于ActivityFragmentActivity提供了使用Fragment的能力。
  • AppCompatActivity基于FragmentActivity,为ActionBar提供功能。


2019:使用AppCompatActivity

在撰写本文时(查看链接以确认它仍然为真),如果您正在使用应用栏,则Android文档建议使用AppCompatActivity

这是合理的:

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.

添加工具栏的一般说明是

  • 添加v7 appcompat支持库
  • 使您的所有活动扩展AppCompatActivity
  • 在清单中声明要NoActionBar
  • 在每个活动的xml布局中添加ToolBar
  • 在每个活动的onCreate中获取ToolBar
  • 有关更多详细信息,请参见文档说明。他们很清楚而且很有帮助。


    对于最低API级别15,您需要使用AppCompatActivity。因此,例如,您的MainActivity看起来像这样:

    1
    2
    3
    4
    public class MainActivity extends AppCompatActivity {
        ....
        ....
    }

    要使用AppCompatActivity,请确保已下载Google支持库(可以在工具-> Android-> SDK管理器中进行检查)。然后只需将gradle依赖项包含在应用程序的gradle.build文件中:

    1
    compile 'com.android.support:appcompat-v7:22:2.0'

    您可以将此AppCompat用作您的主要Activity,然后将其用于启动Fragments或其他Activity(这取决于您所构建的应用程序类型)。

    BigNerdRanch书是很好的资源,但是是的,它已经过时了。阅读它可获得有关Android工作原理的一般信息,但不要指望它们使用的特定类是最新的。


    Activity类是基本类。 (原始)它支持片段管理(自API 11开始)。不再推荐使用它,因为它的专业性要好得多。

    ActionBarActivity暂时替代了Activity类,因为它很容易在应用程序中处理ActionBar。

    AppCompatActivity是新的处理方式,因为不再鼓励使用ActionBar,而应改用Toolbar(当前是ActionBar的替代品)。 AppCompatActivity继承自FragmentActivity,因此,如果需要处理片段,则可以(通过片段管理器)进行处理。 AppCompatActivity适用于ANY API,不仅16岁以上(谁这么说?)。您可以通过在Gradle文件中添加compile 'com.android.support:appcompat-v7:24:2.0'来使用它。我在API 10中使用了它,效果很好。


    这里有很多混乱,特别是如果您阅读过时的资料。

    基本的是Activity,它可以显示片段。如果您使用的是Android版本> 4,则可以使用此组合。

    但是,还有一个支持库,其中包含您提到的其他类:FragmentActivityActionBarActivityAppCompat。最初,它们用于支持版本小于4的Android版本上的片段,但实际上,它们还用于向后移植较新版本的Android(例如材料设计)中的功能。

    最新的一个是AppCompat,另外两个是较旧的。我使用的策略是始终使用AppCompat,以便在未来版本的Android向后移植的情况下可以使用该应用程序。


    如果您谈论ActivityAppcompactActivityActionBarActivity等,等等。

    我们需要谈论它们正在扩展的基类,首先,我们必须了解超类的层次结构。

    所有事情都是从上下文开始的,它是所有这些类的超类。

    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

    Context后跟或扩展ContextWrapper

    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.

    现在我们到达Activity

    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 V14

    The
    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.

    当使用此类而不是新平台的内置片段和加载程序支持时,必须分别使用getSupportFragmentManager()getSupportLoaderManager()方法来访问这些功能。

    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添加到活动中,方法是扩展活动的此类并将活动主题设置为Theme.AppCompat或类似主题

    Here

    我指的是这两个


    由于名称可能会在将来的Android版本中更改(当前的最新名称是AppCompatActivity,但可能会在某个时候更改),我认为最好是扩展AppCompatActivity的类Activity您的所有活动都从那开始。如果明天,他们将名称更改为AppCompatActivity2,例如,您将只能在一个地方更改它。


    AppCompatActivity扩展FragmentActivity扩展BaseFragmentActivityApi16扩展BaseFragmentActivityApi14扩展SupportActivity扩展Activity

    因此,Activity比所有功能都快,而AppCompatActivity是最好的。