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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:21:28+00:00 2026-06-17T19:21:28+00:00

If I have an Activity which is in the foreground and I navigate to

  • 0

If I have an Activity which is in the foreground and I navigate to another Activity within my app, I can detect this by setting a flag when you begin the transfer and wiping it when you have transferred to the new activity. This allows me to distinguish between an activity onPause due to an internal (flag set) or external (flag not set) event.

However, I am having trouble doing this for PendingIntents embedded in Notifications. Is it possible to detect that my Activity is being onPaused because they selected the notification I created on the notification bar? Is there some kind of notification listener I can use which will trigger before the notification fires and the pending intent is executed which onPauses my Activity?

I appreciate that this is somewhat confusing, so I’ve made a skeleton project which demonstrates the problem:

package com.example.notificationtest;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity {

private static final int INCOMING_NOTIFICATION_ID = 1;
private static final String TAG = "NotificationTest";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public void onPause() {
    super.onPause();
    Log.e(TAG,"onPause");
}

@Override
public void onResume() {
    super.onResume();
    Log.e(TAG,"onResume");
}

public void onTransitionButtonClick(View view) {
    Intent intent = new Intent(this, MainActivity.class);
    Log.e(TAG,"About to start activity: onPause will be invoked.");
    startActivity(intent);
}

public void onNotificationButtonClick(View view) {
    Intent intent = new Intent(this, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    Notification notification = new Notification(android.R.drawable.alert_dark_frame, "Alert!", System.currentTimeMillis());
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notification.defaults |= Notification.DEFAULT_LIGHTS;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.flags |= Notification.FLAG_INSISTENT;
    notification.setLatestEventInfo(this, "Click to open", "Click to open", pendingIntent);

    // Show the alert.
    final NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(INCOMING_NOTIFICATION_ID, notification);
}

}

With the activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/notify_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="@dimen/padding_medium"
    android:text="Make a notification"
    android:onClick="onNotificationButtonClick"
    tools:context=".MainActivity" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/notify_button"
    android:padding="@dimen/padding_medium"
    android:text="Normal transition"
    android:onClick="onTransitionButtonClick"
    tools:context=".MainActivity" />

</RelativeLayout>

If you select the “Normal transition” button, the log prints “About to start activity: onPause will be invoked.” prior to onPause.

If you select the “Make a notification” button and then drag down the notification bar and tap on the notification, I want to be informed of that tap prior to onPause so I can insert the same line “About to start activity: onPause will be invoked.”. How can I do that?

  • 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-17T19:21:29+00:00Added an answer on June 17, 2026 at 7:21 pm

    beside holding intent to start activity, PendingIntent can hold also intent to “fire” a broadcast.

    you can use such custom broadcast as trigger to the notification, then implement BroadcastReceiver that will be registered to that broadcast, and will show whatever you want , and only then will start the desired activity.

    Intent intent = new Intent("my.custom.broadcast.action");
    pendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    

    the receiver will look something like this:

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            //do whatever you want
            // if this code is being executed - it's a sign that the user clicked on the notification...
    
            Intent intent = new Intent(this, MainActivity.class);
            Log.e(TAG,"About to start activity: onPause will be invoked.");
            startActivity(intent);
    
        }
    };
    

    don’t forget to register the mReceiver when onCreate() invokes, and unregister it onDestroy()

    registerReceiver(mReceiver , new IntentFilter("my.custom.broadcast.action"));
    unregisterReceiver(mReceiver);
    

    in this approach you can control exactly what will happen when the user click on the notification. as you can see – it’s possible only to send broadcast when notification clicked. no one said you must start activity at all..

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

Sidebar

Related Questions

I need to have Receive activity which can receive my custom data. I found
I have an activity which works as a pager. In this activty there are
I have an activity which have a method in it. In this method I
I have an Activity which I start like this: public class MyProblemsActivity extends ListActivity
I have a BroadcastReceiver that I'm using to send data to another activity which
I have Activity within which i am opening intents for camera ,voice and video
I have an activity which is displaying a web page using a WebView. Within
I have an activity which is going to switch around between several views, and
I have a activity which has a button and 2 autocomplete widget. for the
I have an Activity which shows a welcome message if started for the first

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.