简体   繁体   English

android中的back press和home按钮之间的区别

[英]Difference between back press and home button in android

I am starting a service in my app. 我正在我的应用程序中启动服务。 On click of service I am launching an activity. 点击服务我正在启动一项活动。 The service click event works fine when we go to any app and press back button and exit the app. 当我们转到任何应用程序并按下后退按钮并退出应用程序时,服务点击事件正常工作。

But if we are in any app and then press home button and click on the service the activity is not launched. 但是,如果我们在任何应用程序中然后按主页按钮并单击该服务,则不会启动该活动。 If I click it more then 2 times,it opens the activity and sometimes it opens the app also. 如果我再单击它2次,它会打开活动,有时它也会打开应用程序。

So i am unable to understand the difference between pressing back button and home button. 所以我无法理解按下后退按钮和主页按钮之间的区别。

After you start an activity, if HOME key is pressed, then the current activity is stopped and its task goes into the background. 启动活动后,如果按下HOME键,则当前活动将停止,其任务将进入后台。 The system retains the state of the activity - ie onSaveInstanceState will be called. 系统保留活动的状态 - 即将调用onSaveInstanceState。 If the user later resumes the task by selecting the launcher icon that began the task again, the task comes to the foreground and resumes the activity at the top of the stack. 如果用户稍后通过选择再次启动任务的启动器图标来恢复任务,则任务将到达前台并恢复堆栈顶部的活动。

However, if BACK key is pressed, the current activity is popped from the stack and destroyed. 但是,如果按下BACK键,则会从堆栈中弹出当前活动并将其销毁。 The assmuption is the activity is done and will not be used again. 结束是活动已完成,不会再次使用。 So the system does not retain the activity's state - ie onSaveInstanceState will not be called. 因此系统不会保留活动的状态 - 即不会调用onSaveInstanceState。

Home Task : 家庭作业 :

Pressing the Home switches you from the app to the home screen, whilst leaving your app running in the background. 按Home可将您从应用程序切换到主屏幕,同时让您的应用程序在后台运行。 This is a bit like switching between windows on a Windows PC. 这有点像在Windows PC上的窗口之间切换。

Except that when your phone is running low on resources like memory it will start to close apps that are running in the background, so that your phone has enough resources for what you're trying to do now. 除非您的手机在内存等资源上运行不足,否则它将开始关闭在后台运行的应用,以便您的手机拥有足够的资源来应对您现在要做的事情。 Games are often amongst the first apps the phone will "kill" to save resources as they often use a lot more memory and CPU than other apps. 游戏通常是手机为了节省资源而“杀死”的第一批应用程序,因为它们通常比其他应用程序使用更多的内存和CPU。 This is why sometimes your game is still running paused, and sometimes Android has closed it for you. 这就是为什么有时你的游戏仍在暂停运行的原因,有时Android会为你关闭它。

The Back button is the way to close apps so that they are actually closed. “后退”按钮是关闭应用程序以便实际关闭的方法。

在活动A中启动活动A时调用onPause()。在活动B中调用后退按钮后,在活动A中调用onResume()。

In case of activities their default implementation is LIFO based in stack and works like: 在活动的情况下,它们的默认实现是基于堆栈的LIFO,其工作方式如下:

On Back button Pressed : finish the current activity by calling stop method. 按下后退按钮 :通过调用stop方法完成当前活动。

On Home button pressed : activity is being paused and then it may either resume if come back to it, otherwise system will call stop() method of activity to save unused resources and utilize memory. 按下Home按钮 :活动正在暂停,然后它可以恢复,如果回到它,否则系统将调用stop()方法的活动来节省未使用的资源和利用内存。

but these functions can be edited by overriding if required. 但是如果需要,可以通过覆盖来编辑这些功能。

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

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