在 Android 中,我如何检测我的活动恢复的原因?

发布于 2024-09-16 11:17:44 字数 335 浏览 0 评论 0原文

我正在开发一个活动,它可以进行一些自己的状态管理。我试图区分以下 onResume 情况:

  1. 新启动
  2. 任务开关(主页按钮长按)
  3. 在同一应用程序中的其他活动
  4. 后唤醒后恢复
  5. 在睡眠方向更改

活动的意图或其他地方是否有某些内容可以帮我区分这些?

为了好奇和一些背景......我想在 4 和 4 上保留我的内部历史堆栈。 5.关于案例2& 3,我会保留相同的当前页面,但删除历史记录(允许正常的后退按钮功能在此时接管)。情况 1 将初始化到活动的内部起始页(并且可以在 onCreate 的帮助下轻松检测到)。

I'm developing an Activity that does some of its own state management. I'm trying to differentiate the following onResume cases:

  1. New launch
  2. task switch (home button long-click)
  3. resume after other activity in the same application
  4. wake-up after sleep
  5. orientation change

Is there something in the Activity's intent, or elsewhere, that can help me differentiate these?

For the curious and some context... I'd like to preserve my internal history stack on 4 & 5. On cases 2 & 3, I would preserve the same current page, but erase the history (allow the normal back button functionality to take over at that point). Case 1 would initialize to the activity's internal start page (and can be detected easily enough with some help from onCreate).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫儰鉨ミ守葔 2024-09-23 11:17:44

活动的意图或其他地方是否有某些东西可以帮助我区分这些?

AFAIK,项目 #4 与 onResume() 无关。

通过 android:configChangesonConfigurationChange() 可以更好地处理第 5 项,尽管您可以通过从 onRetainNonConfigurationInstance() 返回某些内容来“检测”它并通过 getLastNonConfigurationInstance() 查看它是否存在于 onResume() 中。

其他情况不仅仅是三种情况,而且可能是两倍,一旦您开始考虑“被踢出内存以释放 RAM”之类的可能性。

即兴地,感觉就像您做出了一些不幸的架构决策(“内部历史堆栈...擦除历史记录...允许正常的后退按钮功能在此时接管”)。 Android 是围绕许多廉价活动而设计的,而您似乎违反了这一规则。欢迎您这样做,但请记住,Android 对您选择的模式的支持可能是有限的。

Is there something in the Activity's intent, or elsewhere, that can help me differentiate these?

Item #4 has nothing to do with onResume(), AFAIK.

Item #5 would be better handled via android:configChanges and onConfigurationChange() though you could "detect" it by returning something from onRetainNonConfigurationInstance() and seeing if it is there in onResume() via getLastNonConfigurationInstance().

The others aren't just three cases, but probably twice that, once you start taking into account things like "being kicked out of memory to free up RAM" as a possibility.

Off the cuff, it feels like you made some unfortunate architectural decisions ("internal history stack...erase the history...allow the normal back button functionality to take over at that point"). Android is designed around lots of cheap activities, and you appear to be violating that precept. You are welcome to do so, but bear in mind that Android support for your chosen pattern may be limited.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文