"MainActivity" 是 activity 的特殊类型吗,它与其他 activity 有什么不同吗?

is the "MainActivity" special kind of activity, does it differ from any other activity?

除了是应用程序启动时的第一个Activity,main activity还有什么特别之处吗? 来自:https://developer.android.com/codelabs/android-training-create-an-activity#0

An app usually consists of multiple screens that are loosely bound to each other. Each screen is an activity. Typically, one activity in an app is specified as the "main" activity (MainActivity.java), which is presented to the user when the app is launched. The main activity can then start other activities to perform different actions.

根据上面的引述,在我看来我们有以下层次结构:

但进一步说:

Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the "back stack"). When a new activity starts, that new activity is pushed onto the back stack and takes user focus. The back stack follows basic "last in, first out" stack logic. When the user is done with the current activity and presses the Back button, that activity is popped from the stack and destroyed, and the previous activity resumes.

这是否也适用于 "MainActivity"?如果 "MainActivity" 被销毁,是否会导致应用程序崩溃,即 MainActivity 的生命周期是否以任何方式不同于任何其他 activity 的生命周期? App退出时MainActivity是最后一个activity停止的吗?

为什么我需要这个:

我想在App退出时释放一些资源(在onStop()方法中(因为post蜂巢保证onStop会被调用)),尤其是ExecutorServices,我在这里读到,即使应用程序已退出,也不能保证 ExecutorService 会停止并使 JVM 继续 working/running,即使应用程序是 closed/killed ] 并将继续使用系统资源。

Main Activity 是用户按下冷启动图标时您的应用程序的入口点。您通过 Intent Filter 将 Activity 中的任何一个作为 AndroidManifest.xml 文件中的主要 activity。 Intent Filter 告诉系统哪个 activity 是主要的。

虽然 main activity 通常被认为是第一个入口点,但请记住,Main Activity 并不总是第一个 activity 启动,例如有各种意图可以分配给您的其他 Activity 和 activity 的过滤器可以在相关操作后直接打开。请阅读 Intent-Filter here.

例如,您的应用是图库应用,典型的首屏是相册列表。您可以在 PhotoActivity 中查看单张照片。这张照片Activity 可以通过外部应用程序的 Intent 直接打开以查看特定照片,而无需启动主 activity。 (查看 google 照片应用)

关于 ExecutorServices 或其他服务生命周期,这里有几个选项:

  1. 实施所有权机制,即启动服务的activity负责关闭服务
  2. 您可以监控应用的 activity 堆栈并在 Activity 堆栈为空时终止服务。
  3. 利用应用程序 class 生命周期来监控事物。
  4. 此处合理讨论