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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:26:08+00:00 2026-06-04T09:26:08+00:00

I have an android app that has mutliple activities The first main activity starts

  • 0

I have an android app that has mutliple activities
The first main activity starts background music using media player. How can I make this music continue playing while the app is im forground
I tried to stop the app on the onpause method and starts it on the onresume method but its cumbersome to have the same code in every activity
Thanks a lot

  • 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-04T09:26:10+00:00Added an answer on June 4, 2026 at 9:26 am

    You cannot stop the Media Player in onPause if you want it to play untill the Application is in foreground. The reason is If you transit from Activity A to Activity B, the onPause of Activity A and onResume/onCreate of Activity B will be called. To achieve the things you want to implement, you will need to check when the application goes in the background. To do so, use the following functions :

    public boolean isAppOnForeground(final Context context)
        {
            final ActivityManager activityManager = (ActivityManager)     context.getSystemService(Context.ACTIVITY_SERVICE);
        final List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null)
        {
            return false;
        }
        final String packageName = context.getPackageName();
        for (final RunningAppProcessInfo appProcess : appProcesses)
        {
            if ((appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) && appProcess.processName.equals(packageName))
            {
                return true;
            }
        }
        return false;
    }
    

    And for Background :

    public static boolean isApplicationBroughtToBackground(final Context context)
    {
            final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            final List<RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty())
        {
            final ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(context.getPackageName()))
            {
                return true;
            }
        }
        return false;
    }
    

    To call this functions, Create a Base Activity and override their onPause() and onResume() method, Extend the Base Activity in place of Activity.

    Hope this helps you.

    Shraddha

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

Sidebar

Related Questions

I'm creating android app that has table layout for the main activity, and that
if you have an android app that has more than one Activity, and Activity
I have a superclass for my Activities (WhereWolfActivity) in my Android app that has
I have an android app that has various activities. for example there is a
I'm building an app that has a main Activity that takes advantage of the
I have a simple android app that has quite a number of buttons, all
I am working on an android app that has two activities. One is the
I have an Android app that has a Content Provider with an SQLiteDatabase implementation.
I have an android app that has to load very large files on start
I'm developing an android app that has a bunch of screens (activities) that are

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.