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.

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?

推荐答案

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.

Android相关问答推荐

如何解决Android Studio中的in fragment问题

将Android Studio插件复制到离线网络

在不增加父行宽度的情况下添加延迟行或可滚动行

Jetpack Compose:如何将浮动操作按钮上方的子按钮居中对齐?

登录方法显示为空列表,即使它显示的是Firebase身份验证控制台

在Jetpack Compose中的隐藏状态栏后面绘制

在Jetpack Compose中,如何判断屏幕是否已重新组合?

Kotlin - 在继续之前如何等待这个协程完成?

仅当先前输入为 yes 时,Android 才会要求下一个输入

使用 async 向网络发出并行请求并在supervisorScope中处理它们

我的自定义小吃店不适合我的全宽屏幕尺寸

如何在 React Native 中调试网络响应

在 Jetpack Compose 中重用具有重复代码的列

ImageBitmap 使用 Glide/Coil 到画布

Android:appcompat 和 material 如何从默认创建 appcompat 和 material 视图?

如何在 Android 上移动 EditText 上的图标?

0dp 大小的可组合文件是否可以组合?

Android YouTube SDK - 视频无法在某些 Android 设备中播放

Android Jetpack Compose:如何让文本利用完整的行空间并将单词换行以防溢出?

从 Android Studio 中移除项目