有没有办法在 onResume 中区分由于 Intent 和其他情况而启动的活动
有什么方法可以区分 onResume() 中的两种情况:
1) Activity 被带到前面,因为它是由其他 Activity 的 Intent 启动的
2)在没有新意图的情况下调用活动的 onResume (例如屏幕关闭和打开,或者可能是用户未明确启动活动的其他情况)
在第一种情况下,我想做一些长时间的操作,例如从网络加载数据。
在第二种情况下,我不想执行任何操作(因为用户没有明确询问,但屏幕只是第二次关闭)
我的意思是,当活动没有重新创建并且 onCreate 没有被调用时的情况。 (如果重新创建,我无论如何都需要进行长时间操作)
据我在这两种情况下尝试, getIntent() 返回原始意图。在这两种情况下 onNewIntent 都不会被调用。
谢谢。
Is there any way to distinguish while in onResume() between two cases:
1) activity is brought to the front because it was started by Intent from other activity
2) activity's onResume called without new Intent (for example screen was off and on, or maybe other situations when activity was not started by user explicitly)
In first case I want to do some long operations, for example load data from web.
In second case I dont want to do nothing (because user didn't ask explicitly, but screen just went off for second)
I mean situations, when activity is not recreated and it's onCreate is not called. (if recreated, I need to do long operation anyway)
As far as I tried in both cases getIntent() returns original intent. In both cases onNewIntent is not called.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此处检查状态图:
http://developer.android.com/reference/android /app/Activity.html
两种情况之间存在一些差异。特别是,onPause、onStop 和 onRestart 仅在应用程序恢复时才会调用。我认为最好的选择是在所有这些方法中打开布尔标志,该标志在 onCreate 时关闭。
Check the state diagram here:
http://developer.android.com/reference/android/app/Activity.html
There are several differences between both situations. Particularly, onPause, onStop and onRestart are only called when the app is resumed. I think your best bet is to turn on a boolean flag in all of these methods, which is turned off at onCreate.