简体   繁体   English

按下按钮 Home i Back 时如何完成应用程序?

[英]How to complete the application when pressed buttons Home i Back?

How to complete the application when pressed buttons Home and Back?按下主页和返回按钮时如何完成应用程序? Provided that in my memory holds many pictures.前提是我的记忆中有很多照片。 When pressed on the Back button - application restarts... When pressed on the Home button - quit application, but when it restart - does not start Splashstsreen.当按下后退按钮时 - 应用程序重新启动...当按下主页按钮时 - 退出应用程序,但当它重新启动时 - 不会启动 Splashstsreen。

Hard to see without code but it sounds like your activity is resuming rather then starting from scratch (as it should behave).没有代码很难看到,但听起来您的活动正在恢复而不是从头开始(因为它应该表现)。 It sounds like it's behaving correctly in that case.听起来在这种情况下它的行为是正确的。 If you insist in wanting your application to truly quit after the back button is clicked perhaps you can override onBackPressed() then have your Activity call its finish() method.如果您坚持希望您的应用程序在单击后退按钮后真正退出,也许您可​​以覆盖 onBackPressed() 然后让您的 Activity 调用其 finish() 方法。

I don't think it's good programming practice to fiddle and interfering with the activities lifecycle.我认为摆弄和干扰活动生命周期不是很好的编程实践。 It's the OS responsibility to manage the lifecycle, including pause and finish activities.管理生命周期是操作系统的责任,包括暂停和完成活动。 Instead you should use other methods to handle your problem with not showing splash screen, these methods are onResume and maybe also onStart().相反,您应该使用其他方法来处理不显示启动画面的问题,这些方法是 onResume,也可能是 onStart()。 Also you should get familiar with the activity lifecycle(link submitted by @ss1271).此外,您应该熟悉活动生命周期(@ss1271 提交的链接)。

Press Home will let the activity to enter onPause() .按 Home 会让活动进入onPause() however, if you insist to quit the application when press HOME, which is obviously not a good practise, you can do like this:但是,如果您在按 HOME 时坚持退出应用程序,这显然不是一个好习惯,您可以这样做:

  1. Override onPause() and onBackPressed()覆盖onPause()onBackPressed()
  2. add finish();添加finish(); to these methods.到这些方法。

I am sure by adding finish();我确定通过添加finish(); to onBackPressed() will quit the app when Back button pressed, but I haven't tried on Home. to onBackPressed()将在按下后退按钮时退出应用程序,但我还没有尝试过主页。

Please refer to Activity Lifecycle for more info.有关更多信息,请参阅活动生命周期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM