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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:27:02+00:00 2026-06-18T10:27:02+00:00

I have an application that contains four activities, within the application the user will

  • 0

I have an application that contains four activities, within the application the user will find himself navigating through the 4 activities constantly. The application also has an ongoing service in the background, the service displays a statusbar notifications, and listens for changes to the content that will then appear on the notification.

Currently, the service displays the notification whenever the user starts an action that required the notification to show, therefore, the notification is shown even when you are still using the application. The desired scenario is to show the notification only when the user has navigated out of the application.

I attempted to override lifecycle methods like this:

    @Override
protected void onPause() {  
    Intent intent = new Intent();
    intent.setAction(MyService.ACTION_DISPLAY_PENDING_NOTIFICATION);
    sendBroadcast(intent);
    super.onPause();
}

@Override
protected void onResume() { 
    super.onResume();
    Intent intent = new Intent();
    intent.setAction(MyService.ACTION_CANCEL_NOTIFICATION);
    sendBroadcast(intent);      
}

And the service goes like this:

@Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (action.equals(ACTION_DISPLAY_PENDING_NOTIFICATION)) {
            showNotification();
        }

        else if (action.equals(ACTION_CANCEL_NOTIFICATION)) {
            mNotificationManager.cancel(mNotId);
        }
    }

This, works. But, since the intent is sent anytime the user navigates away from an activity , I am experiencing an undesired behaviour and slight performance hit when the user navigates through the 4 activities. The service will attempt to display the notification even when going from Activity A to Activiy B, or any combination within the 4 activities.

The notification is immediately cancelled, as when a new Activity B starts it will call mNotificationManager.cancel(mNotifId) during onResume, but the notification was built and shown for a fraction of a second as the service was told to do so when leaving Activity A. That is the behavior I want to address, rather than building and showing this notifications unnecessarily,

Is there any way I can know when the user is leaving the activity to another application, i.e the homepage, etc; but not within the application itself?

EDIT:

Just to clarify, there are two things the activity would have to check for during the onPause method,

a) Is there any previous activity on the foreground? Why? Because the user could navigate out of the activity by pressing back, meaning the last activity on the stack would be displayed. In order to check for this, the answer from DennisDrew would work, we can check like this:

if(!ForegroundHelper.activityExistsInForeground()){
//show your notification
}

But that is not the only way the user could navigate out of the activity, the user could also press the HomeKey, in which case, whether activityExistsInForeground() evaluates to true or false, the notification should be displayed.

b) Is the user going to another activity in the application? For instance, user is on Activity A, A is the only activity on the foreground for now, user clicks on an UI element that launches Activity B. Despite of activityExistsInForeground() evaluating to false, user is not leaving the application, he is launching a new instance of an activity that was previously not on the freground.

I’ve tried to add flags such as private boolean launchingNewActivity = false as default, and setting the flag to true when I know I am going to another activity, say for instance during the click of an item on my listview:

litview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            launchingNewActivity = true
            startActivity2(arg2);
        }           
    });

and then checking for that during onPause:

@Override
protected void onPause() {
    if(!ForegroundHelper.activityExistsInForeground() && launchingNewActivity){
    //show your notification        
}

But doing this, it never shows the notification, somehow the double check always defaults to false.

  • 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-18T10:27:03+00:00Added an answer on June 18, 2026 at 10:27 am

    What if you used a singleton reference? You could make a class like:

    public static class ForegroundHelper {
        public static boolean[] activityStates = new boolean{false, false, false, false};
        public static final int ACTIVITY_A = 0;
        public static final int ACTIVITY_B = 1;
        public static final int ACTIVITY_C = 2;
        public static final int ACTIVITY_D = 3;
    
        public static boolean activityExistsInForeground(){
            for(boolean b : activityStates){
                if(b)
                    return true;
            }
    
            return false;
        }
    }
    

    Then, in each activity’s onResume() do:

    //For your first activity (Activity A)
    ForegroundHelper.activityStates[ForegroundHelper.ACTIVITY_A] = true;
    

    And in each activity’s onPause() do:

    ForegroundHelper.activityStates[ForegroundHelper.ACTIVITY_A] = false;
    

    And then in the onStop() of each activity do:

    if(!ForegroundHelper.activityExistsInForeground()){
        //show your notification
    }
    

    By the way, I haven’t ever done something like this, so I have no idea if it will work exactly as I’ve coded it…but let me know if this helps.

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

Sidebar

Related Questions

I have a silverlight application that contains Bing map. I want when the user
I have an application that contains two Activities with <intent-filter> <action android:name=android.intent.action.MAIN/> <category android:name=android.intent.category.LAUNCHER/>
I have an application that contains 3 activities A, B and C. The activity
I have modular application that contains several modules/plugins, that can also run as standalone
I have an application that contains LoginActivity which makes WebService request to authorize user.
I currently have an application that contains user statuses and support for comments on
I have an application that contains commandLinks within a Primefaces dataTable. The commandLinks link
I have an application that contains a grid and button for the user to
I am building a Rails application that contains Developer s who have Application s.
I have an application that contains many controls on a panel, each with its

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.