java - 我如何追踪我的 Activity 完成的原因?

标签 java android

上下文

我是从事一个非常庞大且非常复杂的项目的开发人员之一。该项目的一半是用 JavaScript 编写的,另一半是用 Java 编写的,此外,大约 30% 的基础产品是预编译的,因此我们无法准确查看或调试它。

问题

当我们的应用程序返回到前台时, Activity 堆栈将被销毁并且仅显示根 Activity 。我无法追查原因。

到目前为止我已经尝试过的事情

  1. 在我能找到的每个 finish() 调用上设置断点,但没有一个被调用
  2. 为每个 Activity 设置 android:excludeFromRecents="false"
  3. 从 AndroidManifest 中删除 android:launchMode="singleTop"
  4. 我在第二个 Activity 的 onDestroy() 方法中设置了一个断点,我可以看到“isFinishing()”方法执行到“True”,所以我知道不是操作系统在破坏 Activity ,但没有信息(据我所知)可以准确指出哪个类/属性/方法导致调用该方法。在堆栈跟踪中我看不到任何有用的东西。我在日志中看不到任何有用的信息。
  5. 我也试过问我公司周围的人,但到目前为止还没有答案。

问题

是否有任何策略可以找到导致特定生命周期方法被调用的原因?

更新 1

感谢您在评论部分提出的重要问题和帮助。下面是序列图,可以更好地解释正在发生的事情。

  1. 应用程序已启动
  2. Activity A 调用 onCreate()
  3. Activity A 调用 onResume()
  4. 我启动 Activity B
  5. Activity A 调用 onUserLeaveHint()
  6. Activity A 调用 onPause()
  7. Activity B 调用 onCreate()
  8. Activity B 调用 onResume()
  9. Activity A 调用 onStop()
  10. 我将应用程序置于后台
  11. Activity B 调用 onUserLeaveHint()
  12. Activity B 调用 onStop()
  13. 我将应用程序返回到前台
  14. Activity B 调用 onDestroy()
  15. Activity A 调用 onRestart()
  16. Activity A 调用 onResume()

更新 2

这里是内存使用情况的截图

当我启动应用程序时 enter image description here

当我启动 Activity B 时

enter image description here

当我将应用程序发送到后台时

enter image description here

当我回到前台时

enter image description here

最佳答案

找到了解决方案!

原来我的根 Activity 设置了以下属性:

android:clearTaskOnLaunch="true"

这是来自 documentation 的引述:

android:clearTaskOnLaunch Whether or not all activities will be removed from the task, except for the root activity, whenever it is re-launched from the home screen — "true" if the task is always stripped down to its root activity, and "false" if not. The default value is "false". This attribute is meaningful only for activities that start a new task (the root activity); it's ignored for all other activities in the task. When the value is "true", every time users start the task again, they are brought to its root activity regardless of what they were last doing in the task and regardless of whether they used the Back or Home button to leave it. When the value is "false", the task may be cleared of activities in some situations (see the alwaysRetainTaskState attribute), but not always.

Suppose, for example, that someone launches activity P from the home screen, and from there goes to activity Q. The user next presses Home, and then returns to activity P. Normally, the user would see activity Q, since that is what they were last doing in P's task. However, if P set this flag to "true", all of the activities on top of it (Q in this case) were removed when the user pressed Home and the task went to the background. So the user sees only P when returning to the task.

If this attribute and allowTaskReparenting are both "true", any activities that can be re-parented are moved to the task they share an affinity with; the remaining activities are then dropped, as described above.

这是一篇帮助我找到它的好文章:http://developer.android.com/guide/components/tasks-and-back-stack.html

关于java - 我如何追踪我的 Activity 完成的原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32171959/

相关文章:

android - 安装后.apk文件在手机中的位置

java - 如何重命名列名称 - JPA Hibernate

java - 如何编译具有Weblogic依赖项的Web项目

java - android套接字DataOutputStream.writeUTF

android - 如何在 map 中心的谷歌地图中获取位置(经纬度/名称)

java - 在android中解析json日期字符串

android - 无法停止/重启 AsyncTask

java - 通过android中的接口(interface)将gcm注册ID从GCMBaseIntentService类传递到MainActivity

java - 找不到 libjvm.so

android - 在哪里可以找到 AOSP 项目中 Android HAL 实际实现的源代码?