In Android, how can I detect why my activity was resumed?

时光毁灭记忆、已成空白 提交于 2019-12-22 12:37:42

问题


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).


回答1:


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.



来源:https://stackoverflow.com/questions/3561588/in-android-how-can-i-detect-why-my-activity-was-resumed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!