Can anyone point me out or explain what is the difference between android Home key and Back key and their respective behavior related to an android app/activity.
Thank you.
Back Key :
If you press Back Key, onPause(), onStop() and onDestroy() callbacks will called.
Activity will created again by system calls onCreate() callback, then onStart() and onResume() callbacks will be followed.
Home Key :
If you press Home Key, onPause() and onStop() callbacks will called.
Here Activity will restart by system calls onRestart() callback, then onStart() and onResume() callbacks will be followed.
In addition to #Fosco's comments, using back will usually cause an app to exit, where home will leave it running. This is dependent on the application, but the general pattern is to exit the app when using back on the last activity.
Back key destroys the current Activity, home key doesn't. In the Activity lyfecycle, pressing back calls all the way to current activity's onDestroy() method. On the other hand, pressing home pauses the Activity, which stays alive in background.
The home key takes you to the home screen, the back key takes you back to the previous activity (or home if there's no activity to go back to.)
If you are at the home screen and launch Messaging, then hit back, it's the same as hitting the home key.
If you're in Email and get an alert for a text message, and you choose the notification which takes you to Messaging, then hit Back, you'll go back to Email.
edit: as mentioned by Tim Coker, when the back button takes you to the home screen, it usually terminates the activity. I think this is based on the app, whether it terminates or stays resident.
Related
On the Android dev page, it says pressing the "Home" or "Overview" button does not invoke onDestroy,
but in my app, it keeps calling onDestroy. Are there any clues?
(detail situation below)
I've built a simple app that switches from the main activity to a second activity,
but if I press the "Home" or "Overview" button on the second activity, the onDestroy gets called.
So when I go back to my app again, it shows the main activity, not the second activity.
Is this normal?
Should I save the state if I want to go back to the last activity (not the main activity) after pressing the Home or the Overview button and coming back to my app?
Android dev page that I read:
If a new activity or dialog appears in the foreground, taking focus and completely covering the activity in progress, the covered activity loses focus and enters the Stopped state. The system then, in rapid succession, calls onPause() and onStop().
and
Note: When the user taps the Overview or Home button, the system behaves as if the current activity has been completely covered.
So it is supposed to invoke only onPause and onStop, not onDestroy, isn't it?
Finally I found the culprit!
the problem was that I set android:noHistory="true" on the second activity, in the AndroidManifest.xml file.
Making this option true let the activity not leave the history,
so if another activity comes to the foreground and the user pushes the back button, the previous activity(noHistory=true) does not show up.
Similarly, if the user pushes the Home or the Overview button, then the user tries to come back to our app, the last activity(noHistory=true) does not show up either.
You have to put the Code in your Question or we can't help you.
Maybe you are calling finish() in MainActivity after you call startActivity(MainActivity.this, SecondActivity.class) ?
Use the edit-function and show us your code then we can help you more.
When you press the home button, what does it do by default?
I want to keep what it does by default, but make sure it ends my music as well.
For example:
public void onBackPressed() {
return;
I disabled my back button.
I want to make it so the home button does what it does, but i want to call my
this.stopService(new Intent(this, Music.class));
in the method too.
It is not possible to override "Home" key press just like you did for Back button press. In fact you're not supposed to over ride home key press because user presses Home key to quickly come to the launcher.
Even if you try to override onKeyDown(), you'll see that Home key press will not invoke this function.
But pressing Home key will surely call onStop() of your activity and hence you can stop your music service in onStop(). Hope this helps.
When you press the home button, what does it do by default?
It brings the home screen activity to the foreground.
I want to keep what it does by default, but make sure it ends my music as well.
Stop the music in onPause() or onStop() of your foreground activity, as these will be called when it loses the foreground to the home screen activity.
If you are talking in coding sense -
The home button brings the default launcher to the foreground.
Call onPause() or onStop with super.onPause() or super.onStop() inside of the method.
If you are talking in literal terms with nothing to do with code then..
The home button launches the default launcher activity, this can be cleared if you installed the launcher on some devices by going settings > applications > manage find the application and then go clear defaults
When the music stops is decided by the developers code, you cant change how it works sorry.
I am really confused. I have read that the back button
calls onDestroy()
can close up your currently-running activity
calls onPause()
I think onPause() should be right. But this is an side effect, because the Activity gets into the background. I found nothing in the docs. But maybe I have overlooked something.
Can someone please explain to me what the back button is supposed to do programmatically? Some references would also be nice. :-)
I have read that the back button calls onDestroy(), can close up your currently-running activity, calls onPause()
All three are correct.
I found nothing in the docs.
Quoting the Android documentation:
When the user presses the BACK key, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored).
To elaborate, if there is nothing else that will consume the BACK button press (e.g., an open options menu), your Activity will be called with onBackPressed(). The default implementation of this calls finish(). This will take your activity from the running to the destroyed states, calling onPause(), onStop(), and onDestroy() in sequence, as shown in the event flow diagram:
Just to add, browser application overrides onBackPressed() to go back to previously opened tabs (if available) and it not, closes the application.
I need perform some action on press of home key like starting activity when some one presses the home key...how to do it?
If what you really want is to perform some actions when your activity leaves the foreground, see the activity lifecycle. Your activity will have its onPause method called when it is no longer in the foreground, and onStop called when it is no longer visible.
Let's say I have a Hello World single Activity application. I start this application.
What methods are invoked in each case:
Home button is pressed: ?
Back button is pressed: ?
Phone call is received: ?
What methods are invoked once the user starts the application again via the app icon (assuming the OS hasn't had a "other apps need memory condition"):
Home button was pressed: ?
Back button was pressed: ?
Phone call was received: ?
Thanks all.
Edit: Extra Credit: How can the user invoke onPause without invoking onStop?
both pressing home button and receiving a call don't remove the activity from the task's stack, and will be available when you re-enter the app => onPause() => onStop().
as the activity lifecycle diagram shows, re-entering the app calls => onRestart() => onStart() => onResume()
pressing the back button instead kills the activity => onPause() => onStop() => onDestroy()
re-entering the app in this case calls the classics => onCreate() => onStart() => onResume()
EDIT
from http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
If an activity has lost focus but is
still visible (that is, a new
non-full-sized or transparent activity
has focus on top of your activity), it
is paused. A paused activity is
completely alive (it maintains all
state and member information and
remains attached to the window
manager), but can be killed by the
system in extreme low memory
situations.
There can be several scenarios
Opening the app from the app icon. following methods are called
onCreate()-->onStart()-->onResume()
When user presses the home button
onPause()-->onStop()
When user returns to the app from the Activity Stack
onRestart()-->onStart()--> onResume()
When the app is running and user presses the power button
onPause()-->onStop()
When user unlocks the phone
onRestart()-->onStart()--> onResume()
When user gets an incoming call while you are in the app
onPause()
When user returns to the app after disconnecting the phone call
onResume()
When user presses the back button from the app
onPause()-->onStop()-->onDestroy()
And when the user presses the home button and from the activity stack user swipes the app.onDestroy() method may or may not be called depending upon the OS contains the context of the Activity or not according to memory requirements
Well see, while a sequence of events may occur with your hello world program, the same sequence may not occur in say a video game, because Android will probably Destroy it for taking up too much resources.
The best way I have found to see the lifecycle for my app is to override all the methods (onStart, onRestart,..., including the onSaveInstance and onRestoreInstance) and insert log statements in each one. Like so:
#Override
public void onDestroy() {
// Call the super class
super.onDestroy();
// Log the action
Log.d("Debug", "onDestroy() has been called!");
}
Now I can go to logcat and see what events took place.