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 7408775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:58:19+00:00 2026-05-29T05:58:19+00:00

EDIT – FOUND A EASY 5-10 LINE SOLUTION!!! See MY OWN ANSWER BELOW!!! YAY!!!!!!!!!

  • 0

EDIT – FOUND A EASY 5-10 LINE SOLUTION!!! See MY OWN ANSWER BELOW!!! YAY!!!!!!!!!

I’ve searched for 5 hours, dozens of SO posts, no answers, and this seems like the most simple obvious freaking thing!!!

EDIT- btw, this is NOT a music player app, just an app to view pics and text, opens multiple activities like menu and about, view diff types of pictures etc. I just want to play some simple background music while looking thru the pics and text, why is that so difficult?

another EDIT – it seems the main question really is:
“WHY DOES PRESSING THE HOME BUTTON NOT CALL onPause or onStop ???” -so I can know when to stop the media player? and how do the games I download on the market accomplish this???

home activity starts

then

media player starts:

player = MediaPlayer.create(this, R.raw.idil);
player.setLooping(true);
player.setVolume(100,100);
player.start();

Player declared outside of onCreate

MediaPlayer player;

When other activities are opened, the background music continues uninterrupted, which is GOOD, that is what I want.

Now, when I am done with my very simple app (that just shows some pics and texts in diff activities), I either click BACK multiple times to get to the home/original activity, and then one more time to “exit”, OR, I simply press home to “exit” because I’m DONE with this app and I do not need to hear that music anymore.


OPTION 1

Call player.stop(); in an onPause override, this is not what I want because background music stops when I leave the home activity for other activities like ‘menu’ and ‘about’, I also do not what to use pause and resume when opening new activities because I do not want the pretty background music to ‘skip’ or be interrupted.


OPTION 2

@Override
    protected void onPause() {
        super.onPause();
        if (this.isFinishing()){
            player.stop();
        }
    }

This is better because background music does not stop between activities, and when I press BACK from my home activity, the music stops, and I can continue to enjoy my android fun phone in peace and quiet, but the problem is, when pressing the HOME button to “exit” my app, that pesky background music keeps playing.


ODDLY

@Override
protected void onStop() {
        super.onStop();
        if (this.isFinishing()){
            player.stop();
        }
    }

Does the same as onPause (and I do understand the actual differences)

EDIT- ALSO it doesn’t seem to matter if player.stop(); is above or below super.onStop(); but it affecting something i cant see, either way, still no SOLUTION πŸ™

ooooo

ooooo

EDIT- ANOTHER OPTION- BUT DOES NOT WORK

public void onUserLeaveHint() {
    player.stop();
    super.onUserLeaveHint();
}

this stops the music when i press HOME but it also stops it when i start new activities πŸ™

EDIT – A VERY COMMON WORK AROUND IVE SEEN MULTIPLE PLACES, but seems to ridiculous to have to do:

essentialy keep count of the number of activities that have been opened and closed (onResume and onPause would prob be best, but that points irrelevant) and when that count reaches 0, stop the background music. YES that is pretty simple, but why do I have to programmatically do this, actually the BIGGEST QUESTION FOR THIS POST IS:

WHY DOES PRESSING THE HOME BUTTON NOT CALL onPause or onStop ???

To put it in onDestroy is not an option because onDestroy is only called when the system is low on memory, or you force close your app, and that is well documented.

Overriding the HOME button also is no option as I have read it’s “not possible” and I have read it’s “extremely frowned upon”, either way I’m avoiding that.

I don’t see the need to create SERVICE, and even if I did, when would I stop the service, it seems I would have the same problem

NOW HERE IS THE THING that completely blows my mind, every game and every app I have downloaded from the android market has very beautiful background music, and when I press BACK or HOME because I am done playing that lovely game the music stops, not keeping playing in the background.

I am very frustrated, and I feel it’s because I feel like I am missing something very simple, because this is one of the most basic lifecycle issues with any app.

I spent a month reading every page of developer.android.com including the Dev Guide, the tutorials, sample projects, and researching the Reference section, as well as google-ing this topic for 5 hours.

I also don’t understand why the 6 or 7 SO threads with the exact same issue, have not been answered, every downloadable app on the market stops playing its music (unless its a background music player) when you press HOME or BACK or whatever to “exit” the app to go do something else on your phone.

What am I missing?

  • 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-29T05:58:20+00:00Added an answer on May 29, 2026 at 5:58 am

    I’m very happy today, and still have hair πŸ™‚
    I’ve tested this and it works!!!

    First, add this to your Manifest:

    <uses-permission android:name="android.permission.GET_TASKS"/>
    

    Second, add this to your ‘Home/Main’ Activity/Class:

      @Override
      protected void onPause() {
        if (this.isFinishing()){ //basically BACK was pressed from this activity
          player.stop();
          Toast.makeText(xYourClassNamex.this, "YOU PRESSED BACK FROM YOUR 'HOME/MAIN' ACTIVITY", Toast.LENGTH_SHORT).show();
        }
        Context context = getApplicationContext();
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        if (!taskInfo.isEmpty()) {
          ComponentName topActivity = taskInfo.get(0).topActivity; 
          if (!topActivity.getPackageName().equals(context.getPackageName())) {
            player.stop();
            Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
          }
          else {
            Toast.makeText(xYourClassNamex.this, "YOU SWITCHED ACTIVITIES WITHIN YOUR APP", Toast.LENGTH_SHORT).show();
          }
        }
        super.onPause();
      }
    

    And obviously replace xYourClassNamex with, well, Your Class Name πŸ™‚

    Now obviously you do not need the “Toasts” but it will tell you what is going on.
    The very intersting thind is when you press BACK from your ‘Home/Main’ activity, you obviously get 2 Toasts, “YOU PRESSED BACK FROM YOUR ‘HOME/MAIN’ ACTIVITY”, and the 2nd Toast is “YOU SWITCHED ACTIVITIES WITHIN YOUR APP”. I believe I know why this happens, but it doesn;t matter because I call “player.stop();” from the 2 scenarios that mean my app is no longer being ‘used’. Obviously do more work than “player.stop();” if you need to πŸ™‚ And also obvious you dont need the “else” for “YOU SWITCHED ACTIVITIES WITHIN YOUR APP”, because there is no reason to “stop/pause” the background music, which is what i needed, but if you DO need to do something when new activities are started, well here you go πŸ™‚

    Hope this helps anyone looking to know when the user “leaves/exits/is done with” the app πŸ™‚

    THANKS FOR ALL OF THE COMMENTS POSTS AND HELP EVERYONE!!! YAY!

    EDIT-

    This part has to be in EVERY activity’s onPause:

    Context context = getApplicationContext();
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
            if (!taskInfo.isEmpty()) {
              ComponentName topActivity = taskInfo.get(0).topActivity; 
              if (!topActivity.getPackageName().equals(context.getPackageName())) {
                player.stop();
                Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
              }
            }
    

    so you’ll know if the user left your app from ANY of the activities. this is good to know πŸ™‚

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

Sidebar

Related Questions

Edit: I have solved this by myself. See my answer below I have set
Edit 4: I was finally able to solve my own issue. See checkmark answer
EDIT: looking for this: http://diminishing.org/extending-formtastic-with-a-sprinkle-of-jquery (If this works I'll answer my own question) I've
EDIT: Updated thanks to @daroczig's lovely answer below. However, test 2 still feels like
* EDIT: I found the answer to the memory leak myself, and posted it
Edit: From another question I provided an answer that has links to a lot
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT What small things which are too easy to overlook do I need to
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
Edit: The below question was answered by this . I have a new updated

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.