Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: What Intent Flag to use so undestroyed Activity will not automatically show upon Notification
    text
    copied!<p>I have 3 Activities</p> <p>A. MainActivity - just an Activity with a text and button on it</p> <p>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</p> <p>C. DisplayNotification - activity for displaying notification in the status bar, when clicked, it should show A</p> <p>This is what I want to happen:</p> <ol> <li>I launch my application, A will be shown</li> <li>I click the button, B will be shown</li> <li>I set a time for the alarm</li> <li>When the alarm triggers, a notification is shown in the status bar</li> <li>If I click the notification, A will be shown</li> </ol> <p>This is what happens (I have 2 scenarios with different results):</p> <p>First scenario:</p> <ol> <li>After step 3, I tap the back button, A will now be shown</li> <li>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</li> <li>I wait till the alarm triggers</li> <li>When the alarm triggers, notification is shown on the status bar</li> <li>I click the notification, it launches my app displaying A</li> <li>End of story. This is what is expected to happen</li> </ol> <p>Second scenario: (the buggy one)</p> <ol> <li>After step 3, I tap the HOME button, A's onDestroy is not yet called and the latest screen was B</li> <li>The screen shows the 'home page' or 'desktop' of the phone</li> <li>I wait till the alarm triggers</li> <li>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</li> <li>What I want to happen here is that it should not automatically display B activity.</li> </ol> <p>Am I missing any flags for notification or intent?</p> <p>Here is the code snippet for Activity B which triggers the alarm</p> <pre><code>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); </code></pre> <p>Here is the code for Activity C</p> <pre><code>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(); } </code></pre> <p>I hope someone would help me :)</p> <p>Thanks!</p> <p>Edit:</p> <p>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:</p> <pre><code>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); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload