I want to run code only when the home button (when the app is sent to background) is pressed. I tried using the lifecycle-method but the problem is that they also get executed when and other dialog/activity is started. I only want to check if the application is sent to background. How can I achieve that?
-
Why do you want to do that? – Florian von Stosch Jan 15 '14 at 15:31
-
I have an activity which is password protected..If a user calls this activity and then click the home-button and calls the app again the same activity will be shown. I want to make sure that if the user calls this activity, he should give his password and when he is in this activity and press the home button and start the app again the activity must be started again and not show the same activity..because i can use the password to go in this acitivty and then can sent the activity to backround and then if someone else uses my phone and starts the app he can see the password-protected-activity – androidBeginner Jan 15 '14 at 15:36
7 Answers
In onOptionsItemSelected(MenuItem item);
, check to see if the item clicked is the home button:
EDIT:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

- 8,810
- 2
- 30
- 38
-
sry doesn't work...I only want to finish the current activity when the home button is pressed – androidBeginner Jan 15 '14 at 15:45
-
How are you setting up your ActionBar? Post some code, and we may be able to better help you. If you're trying to finish the current activity when the home button is pressed, just add finish() in the case statement. – Submersed Jan 15 '14 at 15:45
-
Yeah I already have tried it but it didn't work. It doesn't detect the homebutton-event. overriding onUserLeaveHint() worked for me – androidBeginner Jan 15 '14 at 15:55
-
You have to specify display options when setting up your action bar, or similarly use getActionBar().setHomeButtonEnabled(true); – Submersed Jan 15 '14 at 15:58
-
What do you mean by action bar? I just want to run a specific code when the app is sent to backroung (when the home button is clicked). Maybe i am misunderstanding you – androidBeginner Jan 15 '14 at 16:00
Activity.onUserLeaveHint()
fires on your activity when the Activity is going to be backgrounded as a result of user action, such as pressing the home button or switching apps.
-
1sry just checked...it doesn't work properly...this method is also called when another acitivity is started – androidBeginner Jan 15 '14 at 16:59
Well since you are writing the code you can set some boolean flags when you start explicitly another acitivty, and check that when your activity goes through onPause()...
if they are false, it means someone else is pausing your activity.
Maybe not the most elegant solution but it will work if that is the only problem you have.

- 2,925
- 3
- 23
- 32
-
I am using boolean flags but I can't use onePause() because onPause() will be also called when I am using the app and start another activity for example..I don't want to write always the password while using the app but only when app is sent to backround and started again – androidBeginner Jan 15 '14 at 15:41
-
You need to check the boolean in `onPause()`. If it's false, you know that another Activity isn't starting. – Tunga Jan 15 '14 at 19:42
You could have a tracking system (basically a counter) to know when one of your activities is resumed and paused, thus allowing you to know if any of your activities is open.
This way you could know if your app is "open". but this would not apply only to the home button, but to the back button (last back to close your app) or even opening another app from the notification bar.

- 623
- 6
- 21
Android OS will kill your application to free resources, it wont stay in the background all the time. Use service if you want your app keep running in the background

- 320
- 5
- 12
-
what do you mean? I don't want to keep my app running in the background..I just finish my current activity when the app is sent in the background(if the home button is clicked) – androidBeginner Jan 15 '14 at 15:47
-
I thought you want to keep your application running in the background. Your first question wasnt clear enough for me, sorry. Now i get it what you really want to do. Ok, put finish(); inside onpause() method because when user clicks home button, your application will call onpause() method. – rutulPatel Jan 15 '14 at 19:36
If you want to do when the home button is pressed and the activity is going background, you can override this on the activity:
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_HOME:
//somthing you want to do
default:
return super.onKeyUp(keyCode, event);
}
}
I am using the KeyUp event because that's when the app goes backgroud, the user can do a longPress and I don't think you want to do the method when the app still open.

- 2,118
- 1
- 24
- 45
this is sort of a cheat answer, but i find it to be much more reliable.
one method that's always called when you press the home button is the surfacedestroyed method for any surfaceviews contained in any of the program's activities.
Putting the code you want to execute there, having the method point to another method you want to execute, or having the method trigger a flag that points to the method you want to execute will do the job.

- 35
- 1
- 6