@@ -16,6 +16,7 @@ user clicks it</li>
1616 <h2>In this document</h2>
1717 <ol>
1818 <li><a href="#Basics">The Basics</a></li>
19+ <li><a href="#HandlingNotifications">Responding to Notifications</a></li>
1920 <li><a href="#ManageYourNotifications">Managing your Notifications</a></li>
2021 <li><a href="#CreateANotification">Creating a Notification</a>
2122 <ol>
@@ -123,6 +124,138 @@ mNotificationManager.notify(HELLO_ID, notification);
123124</ol>
124125
125126
127+ <h2 id="HandlingNotifications">Responding to Notifications</h2>
128+
129+ <p>A central part of the user's experience with a notification revolves around
130+ how it interacts with the application's UI flow. You must implement
131+ this correctly to provide a consistent user experience within your app.</p>
132+
133+ <p>Two typical examples of notifications are provided by Calendar, which can send out
134+ notifications of upcoming events, and Email, which can send out notifications
135+ when new messages arrive. These represent the two recommended patterns for handling
136+ notifications: either launching into an activity that is separate from the
137+ main application, or launching an entirely new instance of the application
138+ showing the appropriate point for the notification.</p>
139+
140+ <p>The following scenario shows how the activity stack should work
141+ in these two typical notification flows, first handling a Calendar notification:
142+ </p>
143+
144+ <ol>
145+ <li>User is creating a new event in Calendar. They realize they
146+ need to copy part of an email message into this event.
147+ </li>
148+ <li>
149+ The user chooses Home > Email.
150+ </li>
151+ <li>
152+ While in Email, they receive a notification from Calendar for an upcoming
153+ meeting.
154+ </li>
155+ <li>
156+ So they choose that notification, which takes them to a
157+ dedicated Calendar activity that displays brief details of the
158+ upcoming meeting.
159+ </li>
160+ <li>
161+ The user has seen enough to know they have a meeting coming up,
162+ so they press the BACK button. They are now returned to Email, which
163+ is where they were when they took the notification.
164+ </li>
165+ </ol>
166+
167+ <p>Handling an Email notification:</p>
168+
169+ <ol>
170+ <li>
171+ The user is currently in Email composing a message, and needs to
172+ check a date in their calendar.
173+ </li>
174+ <li>
175+ The user chooses Home > Calendar.
176+ </li>
177+ <li>
178+ While in Calendar, they receive a notification from Email about a new
179+ message.
180+ </li>
181+ <li>
182+ They select the notification, which brings them to Email with the message
183+ details displayed. This has replaced what they were previously doing
184+ (writing an e-mail), but that message is still saved in their drafts.
185+ </li>
186+ <li>
187+ The user presses BACK once to go to the message list (the typical flow in the
188+ Email app), and press BACK again to return to Calendar as they left it.
189+ </li>
190+ </ol>
191+
192+ <p>In an Email style of notification, the UI launched by the notification
193+ shows the main application in a state representing that notification.
194+ For example, when the Email application comes to the foreground from its
195+ notification, it displays either the conversion list or a specific
196+ conversation depending on whether there are multiple or only one new
197+ email. To achieve this, we want to completely replace whatever current
198+ state the application is in with a new activity stack representing the
199+ new notification state.</p>
200+
201+ <p>The following code illustrates how to show this kind of notification. Of
202+ most interest is the <code>makeMessageIntentStack()</code> method, which constructs
203+ an array of intents representing the app's new activity stack for this state.
204+ (If you are using fragments, you may need to initialize your fragment and
205+ app state so that pressing BACK will switch the UI back to its parent state.)
206+ The core of this is the {@link android.content.Intent#makeRestartActivityTask
207+ Intent.makeRestartActivityTask()} method, which constructs the root activity
208+ of the stack with the appropriate flags, such as
209+ {@link android.content.Intent#FLAG_ACTIVITY_CLEAR_TASK Intent.FLAG_ACTIVITY_CLEAR_TASK}.</p>
210+
211+ {@sample development/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessage.java
212+ app_notification}
213+
214+ <p>In a Calendar style of notification, the UI launched by the notification
215+ is a dedicated activity that is not part of the normal application flow.
216+ For example, when the user receives a Calendar notification, choosing that
217+ notification starts a special activity that displays a list
218+ of upcoming calendar events — this view is available only
219+ from the notification, not through the Calendar's normal user
220+ interface.</p>
221+
222+ <p>The code for posting this type of notification is very straight-forward; it
223+ is like the above, but the {@link android.app.PendingIntent} is for just a single
224+ activity, our dedicated notification activity.</p>
225+
226+ {@sample development/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessage.java
227+ interstitial_notification}
228+
229+ <p>This is not enough, however. Normally Android considers all activities within
230+ an application to be part of that application's UI flow, so simply launching the
231+ activity like this can cause it to be mixed with your normal application back stack
232+ in undesired ways. To make it behave correctly, in the manifest declaration
233+ for the activity the attributes
234+ <code>android:launchMode="singleInstance"</code> and
235+ <code>android:excludeFromRecents="true"</code>
236+ must be set. The full activity declaration for this sample is:</p>
237+
238+ {@sample development/samples/ApiDemos/AndroidManifest.xml interstitial_affinity}
239+
240+ <p>Because of the use of <code>singleInstance</code>, you must be careful about launching
241+ any other activities from this one. These activities will be launched
242+ in their own task, and care must be taken to make sure this interacts
243+ well with the current state of your application's task. This is essentially
244+ the same as switching to the main application as described for the Email style
245+ notification shown before. Given the <code>makeMessageIntentStack()</code>
246+ method previously shown, handling a click here would look something like this:</p>
247+
248+ {@sample development/samples/ApiDemos/src/com/example/android/apis/app/IncomingMessageInterstitial.java
249+ app_launch}
250+
251+ <p>If you don't want to use the <code>singleInstance</code> launch mode for
252+ this activity, an alternative approach is to use <code>android:taskAffinity=""</code>.
253+ This tells Android that the activity should not be treated as part of the
254+ main application flow, so it will not get mixed together with that. All of the
255+ other issues discussed here do still apply, though this would allow you to start
256+ additional activities that are part of this notification task instead of switching
257+ to and replacing the main application task.</p>
258+
126259<h2 id="ManageYourNotifications">Managing your Notifications</h2>
127260
128261<p>The {@link android.app.NotificationManager} is a system service that manages all
0 commit comments