意图启动 Activity 后调用哪个方法?

Which method is called after an intent starts an Activity?

我知道当 activity 首次创建时,它的 onCreate() 方法开始发挥作用。

假设有一个主要 activity 通过意图启动另一个 activity (secondActivity.java),然后第二个 activity 启动主要 activity再次通过意图。我的问题是 onRestart() 方法是在收到 intent 后调用 main activity 还是调用了 onCreate() 方法?

提前致谢!

这是文档中 Android 生命周期的图像:

当您启动第二个 activity onPause() 时,将在第一个 Activity 上调用等 Activity 然后 onCreate() 将在第二个 activity。正如您在上图中看到的,当您按下后退 onRestart() 时,应该在第一个 activity.

上调用

如果您启动一个新的 Intent,将调用 onCreate() 方法。

My question is that whether onRestart() method is called of the main activity after receiving the intent or is onCreate() method called?

这取决于是否会创建主 activity 的新实例。

默认情况下,一个会。在这种情况下,将使用 onCreate() 调用主 activity 的新实例。主要 activity 的 原始 实例保持不变。

但是,通过 Intent 标志(例如 FLAG_ACTIVITY_REORDER_TO_FRONT)或清单设置,您可以安排将主要 activity 的原始实例带回前台。在这种情况下,将使用 onNewIntent() 调用原始实例(为您提供与 startActivity() 一起使用的 Intent,从而使 activity 回到前台)。它也应该用 onRestart()onStart()onResume() 调用,作为返回前台的一部分。