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

时间:2015-07-08 15:32:33

标签: android android-fragments android-activity android-actionbaractivity appcompatactivity

我来自iOS,您可以轻松使用UIViewController。然而,在Android中,事情看起来要复杂得多,某些UIComponents用于特定的API级别。我正在阅读BigNerdRanch for Android(这本书大约有2年历史),他们建议我使用Activity来托管我的FragmentActivities。但是,我认为Activity已被弃用。

因此,对于API级别22(对API级别15或16的最低支持),我应该使用什么来托管组件以及组件本身?是否有所有这些用途,或者我应该几乎完全使用一两个?

9 个答案:

答案 0 :(得分:293)

I thought Activity was deprecated

No.

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 is the baseline. Every activity inherits from Activity, directly or indirectly.

FragmentActivity is for use with the backport of fragments found in the support-v4 and support-v13 libraries. The native implementation of fragments was added in API Level 11, which is lower than your proposed minSdkVersion values. The only reason why you would need to consider FragmentActivity specifically is if you want to use nested fragments (a fragment holding another fragment), as that was not supported in native fragments until API Level 17.

AppCompatActivity is from the appcompat-v7 library. Principally, this offers a backport of the action bar. Since the native action bar was added in API Level 11, you do not need AppCompatActivity for that. However, current versions of appcompat-v7 also add a limited backport of the Material Design aesthetic, in terms of the action bar and various widgets. There are pros and cons of using appcompat-v7, well beyond the scope of this specific Stack Overflow answer.

ActionBarActivity is the old name of the base activity from appcompat-v7. For various reasons, they wanted to change the name. Unless some third-party library you are using insists upon an ActionBarActivity, you should prefer AppCompatActivity over ActionBarActivity.

So, given your minSdkVersion in the 15-16 range:

  • If you want the backported Material Design look, use AppCompatActivity

  • If not, but you want nested fragments, use FragmentActivity

  • If not, use Activity

Just adding from comment as note: AppCompatActivity extends FragmentActivity, so anyone who needs to use features of FragmentActivity can use AppCompatActivity.

答案 1 :(得分:65)

Activity is the base class of all other activities, I don't think it will be deprecated. The relationship among them is:

Activity <- FragmentActivity <- AppCompatActivity <- ActionBarActivity

'<-' means inheritance here. The reference said ActionBarActivity is deprecated, use AppCompatActivity instead.

So basically, using AppCompatActivity is always the right choise. The differences between them:

  • Activity is the basic one.
  • Based on Activity, FragmentActivity provides the ability to use Fragment.
  • Based on FragmentActivity, AppCompatActivity provides features to ActionBar.

答案 2 :(得分:62)

2018:使用AppCompatActivity

在撰写本文时(检查链接以确认它仍然存在),如果您使用的是应用栏,Android Documentation建议使用AppCompatActivity

这是理性的:

  

从Android 3.0(API级别11)开始,使用所有活动   默认主题将ActionBar作为应用栏。不过,app吧   功能已逐渐添加到本机ActionBar中   各种Android版本。因此,本机ActionBar的行为   不同的取决于Android系统的哪个版本的设备   可能正在使用。相比之下,最新的功能被添加到   支持库的工具栏版本,它们可以在任何版本上使用   可以使用支持库的设备。

     

因此,您应该使用支持库的工具栏类   实施您的活动&#39;应用栏。使用支持库   工具栏有助于确保您的应用具有一致的行为   跨越最广泛的设备。例如,工具栏小部件   在运行Android 2.1的设备上提供材料设计体验   (API级别7)或更高版本,但本机操作栏不支持   材料设计,除非设备运行Android 5.0(API级别   21)或更晚。

添加工具栏的一般说明是

  1. 添加v7 appcompat支持库
  2. 让您的所有活动延伸AppCompatActivity
  3. 在清单中声明您需要NoActionBar
  4. 为每个活动的xml布局添加ToolBar
  5. 在每个活动中ToolBar获取onCreate
  6. 有关详细信息,请参阅documentation directions。他们非常清楚,乐于助人。

答案 3 :(得分:48)

For a minimum API level of 15, you'd want to use AppCompatActivity. So for example, your MainActivity would look like this:

public class MainActivity extends AppCompatActivity {
    ....
    ....
}

To use the AppCompatActivity, make sure you have the Google Support Library downloaded (you can check this in your Tools -> Android -> SDK manager). Then just include the gradle dependency in your app's gradle.build file:

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

You can use this AppCompat as your main Activity, which can then be used to launch Fragments or other Activities (this depends on what kind of app you're building).

The BigNerdRanch book is a good resource, but yeah, it's outdated. Read it for general information on how Android works, but don't expect the specific classes they use to be up to date.

答案 4 :(得分:28)

Activity class is the basic class. (The original) It supports Fragment management (Since API 11). Is not recommended anymore its pure use because its specializations are far better.

ActionBarActivity was in a moment the replacement to the Activity class because it made easy to handle the ActionBar in an app.

AppCompatActivity is the new way to go because the ActionBar is not encouraged anymore and you should use Toolbar instead (that's currently the ActionBar replacement). AppCompatActivity inherits from FragmentActivity so if you need to handle Fragments you can (via the Fragment Manager). AppCompatActivity is for ANY API, not only 16+ (who said that?). You can use it by adding compile 'com.android.support:appcompat-v7:24:2.0' in your Gradle file. I use it in API 10 and it works perfect.

答案 5 :(得分:11)

There is a lot of confusion here, especially if you read outdated sources.

The basic one is Activity, which can show Fragments. You can use this combination if you're on Android version > 4.

However, there is also a support library which encompasses the other classes you mentioned: FragmentActivity, ActionBarActivity and AppCompat. Originally they were used to support fragments on Android versions < 4, but actually they're also used to backport functionality from newer versions of Android (material design for example).

The latest one is AppCompat, the other 2 are older. The strategy I use is to always use AppCompat, so that the app will be ready in case of backports from future versions of Android.

答案 6 :(得分:3)

由于该名称可能会在Android的未来版本中发生变化(目前最新版本为AppCompatActivity,但可能会在某些时候发生变化),我认为有一个好处是类Activity扩展AppCompatActivity,然后您的所有活动都从那个扩展。如果明天,他们会将名称更改为AppCompatActivity2,例如,您必须在一个地方更改名称。

答案 7 :(得分:3)

如果您谈论 Activity AppcompactActivity ActionBarActivity 等等。< / p>

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

所有事情都是从Context开始的,这是所有这些类的超类。

  

Context 是一个抽象类,其实现由。提供   Android系统。它允许访问特定于应用程序的资源和   类,以及应用程序级操作的上调,如   发起活动,广播和接收意图等

Context 后跟或延伸 ContextWrapper

  

ContextWrapper 是一个扩展上下文类的类   将其所有调用委托给另一个Context。可以子类化   修改行为​​而不更改原始上下文。

现在我们转到 Activity

  

活动是一个扩展 ContextThemeWrapper 的类,   用户可以做的单一,专注的事情。几乎所有的活动   与用户交互,因此Activity类负责创建一个   窗口给你

以下类仅限于扩展,但它们在内部通过其下行程序进行扩展,并为特定的Api提供支持

  

SupportActivity 是一个扩展Activity的类,它是一个用于组合兼容性功能的Base类

     

BaseFragmentActivityApi14 是一个扩展 SupportActivity 的类   这是一个基类它是受限制的类,但它是延伸的    BaseFragmentActivityApi16 以支持 V14 的功能

     

在    BaseFragmentActivityApi16 是一个扩展的类    BaseFragmentActivityApi14 ,它是{@code的基类    FragmentActivity },以便能够使用 v16 API。但它也是   限制类,但它由FragmentActivity扩展以支持   V16的功能。

现在FragmentActivty

  

FragmentActivity 是一个扩展的类    BaseFragmentActivityApi16 并且想要使用基于支持的   片段和加载器API。

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

  

ActionBarActivity 是支持库的一部分。支持库用于在旧平台上提供更新的功能。对于   例如, ActionBar 是在API 11中引入的,并且是其中的一部分   默认情况下的活动(实际上取决于主题)。相反   旧平台上没有 ActionBar 。所以支持   库添加了一个子类Activity( ActionBarActivity )   提供 ActionBar&#39> 功能和ui

2015年,支持库的修订版22.1.0中不推荐使用 ActionBarActivity 。应该使用 AppCompatActivity

  

AppcompactActivity 是一个扩展的类    FragmentActivity ,它是使用支持库操作栏功能的活动的基类。

通过为您的活动扩展此课程并将活动主题设置为 getSupportLoaderManager() ActionBar >或类似的主题

Here

我引用了这两个onetwo

答案 8 :(得分:0)

AppCompatActivity 扩展 FragmentActivity 扩展 BaseFragmentActivityApi16 扩展 BaseFragmentActivityApi14 扩展 SupportActivity 扩展活动

因此,活动比所有主题都快,并且 AppCompatActivity 是所有主题中最好的。