我来自iOS,它很简单,你只需要使用UIViewController。然而,在Android中,事情似乎要复杂得多,特定的API级别有特定的uiccomponent。我正在阅读BigNerdRanch for Android(这本书大约有2年的历史了),他们建议我使用Activity来托管我的FragmentActivities。然而,我认为活动是不赞成的。

那么对于API级别22(至少支持API级别15或16),我究竟应该使用什么来托管组件,以及组件本身呢?所有这些都有用处吗,还是我应该只使用其中一两个?


当前回答

活动类是基本类。它支持片段管理(API 11以来)。不再推荐使用它,因为它的专门化要好得多。

ActionBarActivity是Activity类的替代品,因为它使得在应用程序中处理ActionBar变得很容易。

AppCompatActivity是新的方式去,因为动作栏不再被鼓励,你应该使用工具栏代替(这是目前的动作栏的替代品)。AppCompatActivity继承自FragmentActivity,所以如果你需要处理片段,你可以(通过片段管理器)。AppCompatActivity适用于任何API,而不仅仅是16+(谁说的?)你可以通过在Gradle文件中添加compile 'com.android.support:appcompat-v7:24:2.0'来使用它。我在API 10中使用它,它工作得很完美。

其他回答

由于名称很可能会在未来的Android版本中发生变化(目前最新的是AppCompatActivity,但它可能会在某个时候发生变化),我相信有一个扩展AppCompatActivity的类Activity是一件好事,然后你所有的活动都从这个类Activity扩展。例如,如果明天他们将名称更改为AppCompatActivity2,你将不得不在一个地方更改它。

对于最低API级别为15的情况,您需要使用AppCompatActivity。例如,你的MainActivity看起来是这样的:

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

要使用AppCompatActivity,请确保您下载了谷歌支持库(您可以在您的Tools -> Android -> SDK管理器中检查这一点)。然后在你的应用的gradle中包含gradle依赖。构建文件:

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

你可以使用这个AppCompat作为你的主活动,然后它可以用来启动Fragments或其他活动(这取决于你正在构建的应用类型)。

《BigNerdRanch》这本书是一个很好的资源,但是是的,它已经过时了。阅读它可以获得关于Android如何工作的一般信息,但不要期望他们使用的特定类是最新的。

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 在Manifest中声明你想要NoActionBar。 在每个活动的xml布局中添加一个工具栏。 在每个活动的onCreate中获取工具栏。

有关详细信息,请参阅文档说明。它们很清楚,很有帮助。

这里有很多困惑,特别是如果你阅读了过时的资料。

最基本的是Activity,它可以显示片段。如果你在Android版本> 4上,你可以使用这个组合。

然而,也有一个支持库,它包含了你提到的其他类:FragmentActivity, ActionBarActivity和AppCompat。最初它们用于支持Android版本< 4的片段,但实际上它们也用于移植新版本Android的功能(例如材质设计)。

最新的一款是AppCompat,其他两款都比较老。我使用的策略是始终使用AppCompat,这样应用程序就可以从未来的Android版本中向后移植。

活动类是基本类。它支持片段管理(API 11以来)。不再推荐使用它,因为它的专门化要好得多。

ActionBarActivity是Activity类的替代品,因为它使得在应用程序中处理ActionBar变得很容易。

AppCompatActivity是新的方式去,因为动作栏不再被鼓励,你应该使用工具栏代替(这是目前的动作栏的替代品)。AppCompatActivity继承自FragmentActivity,所以如果你需要处理片段,你可以(通过片段管理器)。AppCompatActivity适用于任何API,而不仅仅是16+(谁说的?)你可以通过在Gradle文件中添加compile 'com.android.support:appcompat-v7:24:2.0'来使用它。我在API 10中使用它,它工作得很完美。