Android后退按钮无法重新启动Activity?
我的应用程序启动一项活动。单击按钮后,应用程序将打开带有网页的浏览器。当我点击后退按钮时,它会返回到我的初始活动屏幕,但不会恢复或重新启动活动。
当我将所有布局代码和活动代码放在 onResume 而不是 onCreate 中时,活动将重新启动。
我的问题是这是否是正确的做法?我可以使用 onResume 来绘制布局并启动活动,还是这个设计很糟糕?当浏览器启动时,初始活动是否会忘记其布局?
请告诉我你的建议。
谢谢 克里斯
My app intiates an activity. On the click of a button, the app opens up the browser with a webpage. When I hit the back button, it comes back to my initial activity screen, but does not resume or restart the activity.
When I put all the layout code and activity code in onResume instead of onCreate, the activity gets restarted.
My question is whether this is the right way to go about it? Can I use onResume to draw my layout and initiate the activity, or is this poor design? When the browser fires up, does the initial activity forget its layout?
Please let me know what you suggest.
Thanks
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大多数情况下,您应该阅读活动生命周期。
只要只执行一次,就可以在 onResume 中初始化。要么有一个专用的 hasInitialized 成员,要么检查其他具有同等含义的值,并且如果设置了,则不要再次初始化。
Mostly you should read about the Activity Life Cycle.
It is fine to initialize in onResume as long as you only do it once. Either have a dedicated hasInitialized member or check some other value that will have equivalent meaning, and do not initialize again if it is set.