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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:25:57+00:00 2026-06-01T07:25:57+00:00

Hi guys I’m using this method to check if my app is in background

  • 0

Hi guys I’m using this method to check if my app is in background or not:

public static boolean isApplicationSentToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    for( RunningTaskInfo rti: tasks ) {
        ComponentName cn = rti.topActivity;
        LogHelper.i(TAG, "Component Name: " + cn.getPackageName() );
    }



    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }

    return false;
}

I’m calling this method in my Activity onPause, onStop, onDestroy
These is my results:

when I’m in my activity and I click the android-home-button it follow this flow:

onPause -> is in background? true
onStop -> is in background? true

and it’s all perfect.
The problem is when I click the android-back-button that from th activity goes back to the android home. This is the flow:

onPause -> is in background? false
onStop -> is in background? false
onDestroy -> is in background? false

If you check the code I have a Log cat stamp and it always (in the back-button case) stamp this:

03-29 00:46:39.324: I/DFFramework(15344): MusicUtils – Component Name: my-activity-name

This means that my activity is not in the background.
What can I do?

UPDATE:

I’m adding more info about my scenario. I’m developing a Music Player so I’ve to catch this kind of event in order to start my foreground audio service if the app is closed but the music is still playing (otherwise the music will be paused).
So I’ve to catch when the app is in background (like when you click the android-home-button) or when you are closing the application (for example when you click android-back-button).

  • 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-06-01T07:25:58+00:00Added an answer on June 1, 2026 at 7:25 am

    So this is how I solved (to emulate the Google Music Player behaviour)

    As I said I was looking for 2 kinds of event:

    • when my app goes in background

    • when user tap androd-back-button and goes to the android home (app is destroyed)

    So this is my onPause done in my super activity

    @Override
        protected void onPause() {
            boolean bg = MusicUtils.isApplicationSentToBackground(this);
            if ( bg ) {
                MusicUtils.startForegroundMusic(this);
            } else {
                if ( isBackPressed ) {
                    //MusicUtils.getActiviesApplicationInBackStack(this) <= 1
                    if ( isBackPressed && (this instanceof DashBoardActivity || MusicUtils.getActiviesApplicationInBackStack(this) <= 1) ) {
                        MusicUtils.startForegroundMusic(this);
                    }
                }
            }
            super.onPause();
        }
    

    First of all I check if the app is in background with isApplicationSentToBackground (the source code in the question body).

    If the app is not in the background there are 2 reasons:

    • The activity is still in the foreground so the onPause it’s called because we’re starting a new activity or we’re changing orientation (i need to skip this event)
    • The app is going to be destroyed (so the same for the activity) and I need to catch this event in order to start the foregroundMusic

    So to catch the second event I check if the user pressed the back button

    private boolean isBackPressed = false;
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                LogHelper.i(TAG, "BACK PRESSED EVENT");
                isBackPressed = true;
            }
    
            // Call super code so we dont limit default interaction
            return super.onKeyDown(keyCode, event);
        }
    

    And if they bakc-key is pressed I check if this activity is the latest one (so after the current activity is destroyed my app will be destroyed!)

    To check this I get the count number returned by MusicUtils.getActiviesApplicationInBackStack(this)

        public static int getActiviesApplicationInBackStack(final Context context) {
        int count = 0;
        ActivityManager am = (ActivityManager) context.getSystemService( Context.ACTIVITY_SERVICE );
        List<RunningTaskInfo> runningTaskInfoList =  am.getRunningTasks(10);
        Iterator<RunningTaskInfo> itr = runningTaskInfoList.iterator();
        while(itr.hasNext()){
            RunningTaskInfo runningTaskInfo = (RunningTaskInfo)itr.next();
            if ( runningTaskInfo.topActivity.getPackageName().equals(context.getPackageName()) ) {
                count += runningTaskInfo.numActivities;
            }
        }
        return count;
    }
    

    I hope this answer help someone in my same problem 🙂 I know that this is a strange scenario but it’s always a good think to share problems, thoughts and code 🙂

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

Sidebar

Related Questions

Guys, for some reason EL is not telling actions from properties. I have this
Guys i am trying to connect my android app to php server and using
Guys, I've came across this problem I can't resolve myselg, I'm pretty sure I
Guys, I am using SQL Server 2000 and executing the sp_columns stored procedure to
guys which text editor is good for Rubyonrails? i m using Windows and i
guys i have arrays in which i have to match this kind of text
guys I know this question is very basic but I've met in few publications
Guys i am very new to jQuery. I have started using the auto complete
Guys this is driving me crazy.. I am amateur in the mod_rewrite topic.. I
Guys, I am using dynamic programming approach to solve a problem. Here is a

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.