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?
beside holding intent to start activity,
PendingIntentcan hold also intent to “fire” a broadcast.you can use such custom broadcast as trigger to the notification, then implement
BroadcastReceiverthat will be registered to that broadcast, and will show whatever you want , and only then will start the desired activity.the receiver will look something like this:
don’t forget to register the
mReceiverwhenonCreate()invokes, and unregister itonDestroy()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..