简体   繁体   中英

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.

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. The system retains the state of the activity - ie onSaveInstanceState will be called. 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. 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.

Home Task :

Pressing the Home switches you from the app to the home screen, whilst leaving your app running in the background. This is a bit like switching between windows on a 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. This is why sometimes your game is still running paused, and sometimes Android has closed it for you.

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:

On Back button Pressed : finish the current activity by calling stop method.

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.

but these functions can be edited by overriding if required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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