androidBeginner
androidBeginner

Reputation: 197

How to execute code when Home button is pressed?

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?

Upvotes: 1

Views: 1182

Answers (7)

irksomesloth
irksomesloth

Reputation: 35

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.

Upvotes: 0

Tunga
Tunga

Reputation: 1014

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.

Upvotes: 2

Alejandro Cumpa
Alejandro Cumpa

Reputation: 2363

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.

Upvotes: 0

Submersed
Submersed

Reputation: 8870

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);
    }

Upvotes: 2

rutulPatel
rutulPatel

Reputation: 320

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

Upvotes: 0

s1m3n
s1m3n

Reputation: 623

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.

Upvotes: 0

JanBo
JanBo

Reputation: 2943

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.

Upvotes: 0

Related Questions