Android: What Intent Flag to use so undestroyed Activity will not automatically show upon Notification?

Up vote 2 down vote favorite share g+ share fb share tw.

I have 3 Activities A. MainActivity - just an Activity with a text and button on it B. SettingsActivity - this will be displayed if the button on (A) is clicked, this initializes time picker that will send a notification when it alarms C.

DisplayNotification - activity for displaying notification in the status bar, when clicked, it should show A This is what I want to happen: I launch my application, A will be shown I click the button, B will be shown I set a time for the alarm When the alarm triggers, a notification is shown in the status bar If I click the notification, A will be shown This is what happens (I have 2 scenarios with different results): First scenario: After step 3, I tap the back button, A will now be shown Then, I tap again the back button, the onDestroy of A will be called and screen will show the 'home page' or 'desktop' of the phone I wait till the alarm triggers When the alarm triggers, notification is shown on the status bar I click the notification, it launches my app displaying A End of story. This is what is expected to happen Second scenario: (the buggy one) After step 3, I tap the HOME button, A's onDestroy is not yet called and the latest screen was B The screen shows the 'home page' or 'desktop' of the phone I wait till the alarm triggers When the alarm triggers, notification is shown on the status bar AND B was shown automatically without me clicking the notification on the status bar What I want to happen here is that it should not automatically display B activity. Am I missing any flags for notification or intent?

Here is the code snippet for Activity B which triggers the alarm AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); Intent I = new Intent(this, DisplayNotification. Class); i. PutExtra("NotifID", 1); PendingIntent displayIntent = PendingIntent.

GetActivity(getBaseContext(), 0, i, 0); alarmManager. SetRepeating(AlarmManager. RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.

INTERVAL_DAY, displayIntent); Here is the code for Activity C public void onCreate(Bundle savedInstanceState) { super. OnCreate(savedInstanceState); int notifID = getIntent().getExtras(). GetInt("NotifID"); Intent I = new Intent(this, MainActivity.

Class); i. AddFlags(Intent. FLAG_ACTIVITY_SINGLE_TOP); i.

PutExtra("NotifID", notifID); PendingIntent detailsIntent = PendingIntent. GetActivity(this, 0, i, 0); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notif = new Notification(R.drawable. Ic_launcher, "See activity!

", System. CurrentTimeMillis()); notif. Flags |= Notification.

FLAG_AUTO_CANCEL; notif. Defaults |= Notification. DEFAULT_SOUND; CharSequence from = "Notification from me"; CharSequence message = "See activity!

"; notif. SetLatestEventInfo(this, from, message, detailsIntent); nm. Notify(notifID, notif); finish(); } I hope someone would help me :) Thanks!

Edit: Thanks Chirag_CID! I should have used Broadcast Receiver instead of another Activity. So, instead of extending Activity, the DisplayNotification now extends Broadcast Receiver.

This is the onReceive method of DisplayNotification: public void onReceive(Context context, Intent intent) { Log. D(TAG, "DisplayNotification onReceive"); int notifID = intent.getExtras(). GetInt("NotifID"); Intent I = new Intent(context, MainActivity.

Class); i. AddFlags(Intent. FLAG_ACTIVITY_SINGLE_TOP); i.

PutExtra("NotifID", notifID); PendingIntent detailsIntent = PendingIntent. GetActivity(context, 0, i, 0); NotificationManager nm = (NotificationManager) context. GetSystemService(Context.

NOTIFICATION_SERVICE); Notification notif = new Notification(R.drawable. Ic_launcher, "See the quote of the day! ", System.

CurrentTimeMillis()); notif. Flags |= Notification. FLAG_AUTO_CANCEL; notif.

Defaults |= Notification. DEFAULT_SOUND; CharSequence from = "Notification"; CharSequence message = "See the activity! "; notif.

SetLatestEventInfo(context, from, message, detailsIntent); nm. Notify(notifID, notif); } android android-intent notifications android-activity link|improve this question edited Mar 2 at 2:43 asked Feb 29 at 10:56racumin245 100% accept rate.

I read the whole Question 2-3 times.. Now I want to point to few things up there, You have made Activity C in which you are creating notification which will call MainActivity on it's Click. But, When you set alarm from B you are passing intent of Activity C, Though your code of C doesn't have setContentView method, and Also you have written finish() in the end of onCreate() so when Activity C is called through the Alarm set from B, It just call Activity C and set Notification ..but As you written finish() it ends Activity C and shows Activity B latest from the Stack. If You want to cross check.. Write logs in onCreate(),onPause() and onDestroy() of Activity C..and you will see the Activity C is created and Destroyed when Alarm triggered.

Solution: If you don't want to show any activity when you are on Home Screen and Alarm trigger then, In Activity B where you written pending intent for Activity C..You will need it to change to call the Broadcast Receiver and use PendingIntent. GetBroadcast where its PendingIntent. GetActivity and the code you have written in C for notification you will have to write that in Receiver...hence none of your activity will be called when the Alarm Trigger.

Happy Coding :).

Thanks Chirag_CID! I should have used Broadcast Receiver instead of another Activity. So, instead of extending Activity, the DisplayNotification now extends Broadcast Receiver.

This is the onReceive method of DisplayNotification: – racumin Mar 2 at 2:39.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions