Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6156791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:40:22+00:00 2026-05-23T20:40:22+00:00

I am developing an application for music. I have an Activity where I have

  • 0

I am developing an application for music. I have an Activity where I have a list with tracks. I can select a track and then I move to another activity in order to play the selected track. I also have a Service class and when I press the home button, I want it to start the service (play music in the background) and move to the home screen.

Also, I have the method OnBackPressed() in order to move to the previous Activity.

How, I can intercept the home button press? Thanks, for yours answers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:40:23+00:00Added an answer on May 23, 2026 at 8:40 pm

    Rather than catching the home button press, you might want to flesh out the android lifecycle a little more and use onPause() instead. If you override the activity’s onPause() method to start the service, whenever the user leaves the activity temporarily, i.e. goes “home”, that should get you what you need without having to catch all the button presses.

    If you want the service to start whenever the user leaves the activity and, say, goes to another application, the lifecycle is your friend. You might want to consider what happens when the user gets an email notification and leaves your application that way. Do you want the service to start then as well? The user didn’t hit the home button so your service won’t start, but they did leave your application. Do you want the service to start in that scenario?

    It sounds like you want the service to start whenever the user leaves that activity, so please look at overriding onPause() if that is the case.

    More on the android life cycle here:

    http://developer.android.com/reference/android/app/Activity.html

    All you would need is to add this method:

    @Override
    protected void onPause(){
        <start your service here>
    }
    

    Edit: Give this a try as a workaround for not being able to intercept the home button, set a back_flag

    public class Your_Activity extends Activity {
        private boolean back_flag = false;
    
        .
        .
        .
    
        public void onBackPressed(){
            back_flag = true;
            <whatever else you have in onBackPressed()>
        }
    
        @Override
        protected void onPause(){
            if(!back_flag){
                <start your service>
            }
            back_flag = false;
            super.onPause();
        }
    }
    

    Again, if it’s not registering the home, this is the only work around I can think of at the moment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an OS X application to organize things (as iTunes is to music
I am developing application using phonegap in eclipse for android .I have created folder
I'm developing application using VC++ 6. I have a 3rd party DLL. This library
When developing an application which consumes an external webservice I have generated the sources
I'm developing application with NHibernate EntityMode.Map so I have entities of IDictionary without cs
I am developing application for .Epub extension file reader.I have source code available. But
I have been developing Android application since 3 to 4 months. I am naive,
Hi We are developing application in node.js , socket.io , and redis. we have
i am developing application which point towards the particular location, i have calculate angle
i am developing application and i have set some html control which are runat=server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.