I wrote a Notification function to send notificaiton, and it will switch to a defined activity when user click the notification on the top bar. My code did what I want. But there is a problem.
Let's say there are 3 activities (A, B, C) and it away switch to A when user clicks the notification on the top bar.
The flow is:
(1) User open A
(2) Click a button at A and switch to B
(3) Click the notification on the top bar
(4) Switch to A (It is fine)
(5) Again, click a button at A and switch to B
(6) Press Home Button to hide the App
(7) Long press the Home Button and click the App icon
(8) Switch to A (??? It should switch to B as I am on Activity B!)
---- How can I avoid it to switch to Activity A when I switch to my App by long pressing Home Button? Thanks!
Here is the code snippet:
int notificationCounter = 0;
private void displayNotificationMessage(String title, String message, int notification_id, String fromUser, boolean vibrate, boolean playSound, String notificationActionStr)
{
if (receiverMessageBroadcast || !currentTargetUser.equals(fromUser))
{
Intent intent = new Intent();
intent.setClass(this, MainActivity.class);
//intent.setAction(notificationAction + "_" + System.currentTimeMillis());
Bundle bundle = new Bundle();
bundle.putInt("notificationAction", 1);
bundle.putString("targetUser", fromUser);
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
Notification notification = new Notification(R.drawable.notification, message, System.currentTimeMillis());
notification.setLatestEventInfo(this, title, message, PendingIntent.getActivity(this.getBaseContext(), notificationCounter, intent, PendingIntent.FLAG_UPDATE_CURRENT));
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
if (playSound)
notification.defaults |= Notification.DEFAULT_SOUND;
if (vibrate)
notification.vibrate=new long[] {100L, 100L, 200L, 500L};
notification.ledARGB = Color.WHITE;
notification.ledOnMS = 1500;
notification.ledOffMS = 1500;
notificationMgr.notify(fromUser, 0, notification);
notificationCounter++;
}
}