Activity、AppCompatActivity、FragmentActivity 和 ActionBarActivity:何时使用哪个? [英] Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?

本文介绍了Activity、AppCompatActivity、FragmentActivity 和 ActionBarActivity:何时使用哪个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自 iOS,它很容易,您只需使用 UIViewController.然而,在 Android 中事情似乎要复杂得多,特定 API 级别的某些 UIComponents.我正在阅读适用于 Android 的 BigNerdRanch(这本书大约有 2 年历史),他们建议我使用 Activity 来托管我的 FragmentActivities.但是,我认为 Activity 已被弃用.

I'm coming from iOS where it's easy and you simply use a UIViewController. However, in Android things seem much more complicated, with certain UIComponents for specific API Levels. I'm reading BigNerdRanch for Android (the book is roughly 2 years old) and they suggest I use Activity to host my FragmentActivities. However, I thought Activity was deprecated.

那么对于 API 级别 22(最低支持 API 级别 15 或 16),我到底应该使用什么来托管组件以及组件本身?所有这些都有用吗,还是我应该几乎只用一两个?

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 已被弃用

I thought Activity was deprecated

没有

那么对于 API 级别 22(最低支持 API 级别 15 或 16),我到底应该使用什么来托管组件以及组件本身?所有这些都有用吗,还是我应该几乎只用一两个?

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 都直接或间接继承自 Activity.

Activity is the baseline. Every activity inherits from Activity, directly or indirectly.

FragmentActivity 用于向后移植 support-v4support-v13 库中的片段.片段的本机实现是在 API 级别 11 中添加的,低于您建议的 minSdkVersion 值.您需要特别考虑 FragmentActivity 的唯一原因是,如果您想使用嵌套片段(一个包含另一个片段的片段),因为在 API 级别 17 之前,原生片段不支持这种功能.

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 来自 appcompat-v7 库.原则上,这提供了操作栏的向后移植.由于原生操作栏是在 API 级别 11 中添加的,因此您不需要 AppCompatActivity.然而,当前版本的 appcompat-v7 还添加了一个有限的 Material Design 美学,在操作栏和各种小部件方面.使用 appcompat-v7 有利有弊,远远超出了此特定 Stack Overflow 答案的范围.

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 是来自 appcompat-v7 的基础活动的旧名称.由于种种原因,他们想改名.除非您使用的某些第三方库坚持使用 ActionBarActivity,否则您应该更喜欢 AppCompatActivity 而不是 ActionBarActivity.

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.

因此,假设您的 minSdkVersion 在 15-16 范围内:

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

  • 如果您想要向后移植的 Material Design 外观,请使用 AppCompatActivity

如果没有,但您想要嵌套片段,请使用 FragmentActivity

If not, but you want nested fragments, use FragmentActivity

如果没有,使用Activity

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

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

这篇关于Activity、AppCompatActivity、FragmentActivity 和 ActionBarActivity:何时使用哪个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆