What exactly happens when I press home button in Android?
Because when I open it again after home button pressed, it has series of bugs.
I need to know it to track down the point that causes that bugs.
UPDATE:
when pressing home button, application goes to background and onPause() is called and saves the state of UI, however it does not save state of application, like variables, custom views. And you have to save them manually, as Oren explained.
When you press Home Button your Application/Activity goes in background and when you open it again it resume from the same position as it was until it was being killed/closed by the OS.
Activity Life Cycle will give you a clear idea about it.
Technichally? Anything can happen, from just onPause being called, to the device killing the app to free up memory, to the user shutting down and restarting the device. Your app should handle all of these possibilities.
Further reading : android activity lifecycle
Related
Can you please explain to me why my activity which is currently (onStop) is go to OnDestroy event and restarting again after clicking on the app icon in the interface.
Because of that i have problem with user data saving. I'm using event OnDestroy to ask user if he want to save the data. Because of my misunderstanding when i collapse the app and click to it again i am waiting to my activity to have all the data in it but instead i see saving dialog from the previous activity whiсh goes to onDestroy event.
Other apps don't act like this.
What should i check?
Your description is not very clear, so my answer may not help you.
Research the app life cycle here: https://developer.android.com/guide/components/activities/activity-lifecycle.html
Your app will enter onStop is before onDestroy. This can happen if user activates another app. (So the user may no longer see any prompts that you display e.g. To ask to save or not.) Android may choose to destroy your app based on its rules after that point, even if the user clicks on the app icon to start it again.
Perhaps you can test what happens if the user switches back to the app (not clicking the icon), but use the "Recent apps" button and select it.
Android studio home button problem.
I have so many activities, when I press home buttom in any activities.
Then if I restart my app.
it started from splash activity(logo activity).
However, I want to start this from activity where I pressed home button.
Can anybody figure this out?
By default, Android handles this behavior. When you press the home button, the app should go to background and at the next time when you open it, it should start from where you left off. But, Android's memory management is designed to automatically terminate minimized apps that have not been accessed in a while when memory is needed for newly launched apps.
If there is enough memory available and still your app gets terminated, that means you are not using the API's correctly. Please read this [article] to know how to handle onPause() and onResume() to achieve this behavior.
I have made an app.
When I launch that app in the normal way, then it works fine. When I press the home button, the app is paused and is still there as it should be.
Now I want to start my app by an intent...so e.g. when a NFC tag is detected the app should launch. This also works, but when I am pressing the home button then the app disappears, but I would like that it stays open (in pause mode) like when I launch it in the "normal wayy". Does anyone know why this can happen? My observation is that the onStop() function is called but not the onDestroy() function. Therefore it is very strange that the app just "disappears".
Thanks a lot in advance.
Please see Android Activity Lifecycle. onDestroy() is not always called when application goes to background and generally here are no guarantee that onDestory() will be called before app/activity is killed.
Could you please explain what you mean 'pause mode'? You mean that app saves the state?
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 - i.e. 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 - i.e. 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.
onPause() is called in Activity A when it launches Activity B. After the back button is called in Activity B, onResume() is called in Activity A.
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.
I have an app where you enter some data. The PRE-Honeycomb behavior is the following:
back button seems to kill the app, all input data is lost
home button seems to pause the app, all input data is still there after the next start of the app
For Honeycomb the behavior for the home button seems to have changed. After pressing the home button and start the app again all input data is lost. Is that intented? The problem is that I can override the back button to save data, but reading various posts I cannot override the home button. Is that an expected behavior or a bug? How to deal with it?
Any time your app is not visible, it is eligible to be stopped by the system. You shouldn't assume anything about your app still running in the background if it's been hidden. If you want to guarantee data is not lost, make sure you save it somewhere in onPause() and restore it again in onResume().