Android 扩展 LifecycleActivity、Activity、ActionbarActivity 和 AppCompactActivity 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23089547/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:45:20  来源:igfitidea点击:

Difference between extending LifecycleActivity,Activity,ActionbarActivity & AppCompactActivity?

androidandroid-fragmentsandroid-activityandroid-actionbar

提问by Umer Kiani

In Android what is the main difference between extending Lifecycler Activity, Activity,ActionBarActivity & AppCompactActivity? How do these classes differ from each other in terms of usage?

在 Android 中,扩展 Lifecycler Activity、Activity、ActionBarActivity 和 AppCompactActivity 的主要区别是什么?这些类在使用方面有何不同?

回答by stan0

  • extending ActionBarActivitygives you the ActionBars functionality on every API level >= 7
  • by extending Activityyou can avoid adding additional projects/libraries to your project but you'll lack the ActionBaron api levels below 11

edit: More details:

编辑:更多细节:

ActionBarActivityis part of the Support Library. Support libraries are used to deliver newer features on older platforms. For example the ActionBarwas introduced in API 11 and is part of the Activityby default (depending on the theme actually). In contrast there is no ActionBaron the older platforms. So the support library adds a child class of Activity(ActionBarActivity) that provides the ActionBar's functionality and ui

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

edit2: Update April 2015 - it looks like the ActionBarActivityis deprecated in revision 22.1.0of the Support Library. AppCompatActivityshould be used instead.

edit2:2015 年 4 月更新 - 看起来支持库中的ActionBarActivity已弃用revision 22.1.0AppCompatActivity应该改用。

edit3:Update Aug 2017 - LifecycleActivityis a LifecycleOwnerbut:

编辑 3:2017 年 8 月更新 - LifecycleActivityLifecycleOwner但:

"Since the Architecture Components are in alpha stage, Fragment and AppCompatActivity classes cannot implement it (because we cannot add a dependency from a stable component to an unstable API). Until Lifecycle is stable, LifecycleActivity and LifecycleFragment classes are provided for convenience. After the Lifecycles project is released, support library fragments and activities will implement the LifecycleOwner interface; LifecycleActivity and LifecycleFragment will be deprecated at that time."

“由于架构组件处于 alpha 阶段,Fragment 和 AppCompatActivity 类无法实现它(因为我们无法从稳定的组件添加到不稳定的 API 的依赖)。在 Lifecycle 稳定之前,为了方便起见,提供了 LifecycleActivity 和 LifecycleFragment 类。在Lifecycles 项目发布,支持库片段和活动将实现 LifecycleOwner 接口;届时 LifecycleActivity 和 LifecycleFragment 将被弃用。”

(copied from the Architecture Components guideline)

(从架构组件指南中复制)

回答by vanste25

If you look carefully, you will see this

如果你仔细看,你会看到这个

public class ActionBarActivity extends FragmentActivity
implements ActionBarDrawerToggle.DelegateProvider TaskStackBuilder.SupportParentable

Here you can read about FragmentActivity: http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html

在这里您可以阅读有关 FragmentActivity 的信息:http: //developer.android.com/reference/android/support/v4/app/FragmentActivity.html

And differences between Activity and FragmentActivity: Difference between Activity and FragmentActivity

以及Activity和FragmentActivity的区别:Activity和FragmentActivity的 区别

Also, there are some new themes for styling actionBar... https://developer.android.com/training/basics/actionbar/styling.html

此外,还有一些用于样式化 actionBar 的新主题... https://developer.android.com/training/basics/actionbar/styling.html

Actionbar is introduced in API level 11. com.android.support:appcompat-v7:+ is a support library which allows you to have an ActionBar in your app for devices running on Android 3.0 or below. So, if you need actionbar below api level 11 your Activity needs to extend ActionBarActivity.

Actionbar 是在 API 级别 11 中引入的。com.android.support:appcompat-v7:+ 是一个支持库,它允许您在应用中为运行在 Android 3.0 或更低版本上的设备提供 ActionBar。因此,如果您需要 api 级别 11 以下的操作栏,您的 Activity 需要扩展 ActionBarActivity。

If you are targetting api level 11 and above then you don't need to extend ActionBarActivity and reference AppCompat. You can simply extend Activity and you will have actionabr by default.

如果您的目标是 API 级别 11 及以上,那么您不需要扩展 ActionBarActivity 并引用 AppCompat。您可以简单地扩展 Activity,默认情况下您将拥有 actionabr。

Android Studio default project includes it automatically in dependencies and extends ActionbarActivity instead of Activity in order to use it.

Android Studio 默认项目自动将其包含在依赖项中,并扩展 ActionbarActivity 而不是 Activity 以使用它。

回答by danny117

The ActionBarActivityor the SupportActionBarActivityhave additional methods and properties that are not in a generic Activity. for example methods for adding tabs are present in the ActionBarActivityand not in a generic Activity.

ActionBarActivitySupportActionBarActivity有额外的方法和不在一个通用的特性Activity。例如,用于添加选项卡的方法存在于 中ActionBarActivity而不是通用Activity.

The major difference being you don't get an ActionBaron a generic Activity.

主要区别在于您没有获得ActionBar通用Activity.

回答by Matthew Carnaghi

ActionBarActivity just has more support libraries and better usage of the newer themes available from api 11.

ActionBarActivity 只是有更多的支持库和更好地使用 api 11 提供的新主题。

"In its most basic form, the action bar displays the title for the activity and the app icon on the left. Even in this simple form, the action bar is useful for all activities to inform users about where they are and to maintain a consistent identity for your app."

“在最基本的形式中,操作栏显示活动的标题和左侧的应用程序图标。即使在这种简单的形式中,操作栏对于所有活动都非常有用,可以告知用户他们的位置并保持一致您的应用程序的身份。”

回答by Noorul

You are using Android support library When you come to to the Actionbaractivity . so the uses of Support library is your application can be suport for maximum number of devices. Support library gives to your application the power of Backward compatibilty. Actionbaractivity gives you the mulitiple feature like Actionbardrawer toggle etc.. there are more support libraries available. see this link .. and share to your friends...https://developer.android.com/tools/support-library/index.html

当您来到 Actionbaractivity 时,您正在使用 Android 支持库。所以支持库的用途是您的应用程序可以支持最大数量的设备。支持库为您的应用程序提供了向后兼容性的能力。Actionbaractivity 为您提供了多种功能,例如 Actionbardrawer 切换等。还有更多可用的支持库。看到这个链接..并分享给你的朋友...... https://developer.android.com/tools/support-library/index.html